黑马程序员技术交流社区
标题:
求解
[打印本页]
作者:
zhangbo123
时间:
2015-11-5 21:45
标题:
求解
输出 n=5 的螺旋方阵
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
作者:
大自然的搬运工
时间:
2015-11-6 00:04
前两天的笔试题,你看看吧,应该对你有帮助。
/*
* 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印:
* 示例
* 1 2 3 4
*
* 12 13 14 5
*
* 11 16 15 6
*
* 10 9 8 7
*思路:1.给二维数组复制肯定使用循环
* 2.找规律,然后循环
* 3.每一圈为一次大循环,然后四个边各遵循一个规律,总结归路
* 4.打印
*/
public class Test9 {
public static void main(String[] args) {
int n = 6;
int count = 1;
int[][] arr = new int[n][n];
// 循环圈数,以大圈为准,大圈规律,开始数组角标x=y=z;z表示第z-1圈
for (int z = 0; z < (n + 1) / 2; z++) {
// 上横排规律,x不变,y自增
for (int x = z, y = z; y < n - z - 1; y++) {
arr[x][y] = count++;
}
// 右竖排,y不变,x自增
for (int x = z, y = n - z - 1; x < n - z - 1; x++) {
arr[x][y] = count++;
}
// 下横排,x不变,y自减
for (int x = n - z - 1, y = x; y >= z; y--) {
arr[x][y] = count++;
}
// 右竖排,y不变,x自减
for (int x = n - z - 2, y = z; x > z; x--) {
arr[x][y] = count++;
}
}
// 打印
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(" " + arr[i][j]);
}
System.out.println();
System.out.println();
}
}
}
作者:
会长大的小牛
时间:
2015-11-6 21:39
顶个帖子
作者:
申请补助没成功
时间:
2015-11-7 23:09
import java.util.Scanner;
class Demo
{
public static void main(String[] args) {
int size=5;
Scanner sc=new Scanner(System.in);
System.out.println("输入数组大小:");
size=sc.nextInt();
int count=0;
int [][]array=new int[size][size];
int m=0,n=0;
int down,right,up=0,left=0;
down=size-1;
right=size-1;
int max=size*size;
while(true){
count++;
if(m==up){
array[m][n]=count;
n++;
if(n>right){
n=right;
m++;
}
}
else if(n==right){
array[m][n]=count;
m++;
if(m>down){
m=down;
right--;
}
}
else if(m==down){
array[m][right]=count;
right--;
if(right<left){
right=left;
down--;
}
}
else if(right==left){
array[down][left]=count;
down--;
if(down<=up){
down=up;
left++;
}
}
else{
up++;
down=n-1;
right=m-1;
m=up;
n=left;
count--;
}
if(count>=max){
break;
}
}// end loop
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
}
作者:
孙建志
时间:
2015-11-8 00:09
这面试题好难啊,对于我这新手来说基本没戏
作者:
cst1718
时间:
2015-11-8 00:31
马克一下.....
作者:
kingwang
时间:
2015-11-8 14:56
大自然的搬运工 发表于 2015-11-6 00:04
前两天的笔试题,你看看吧,应该对你有帮助。
/*
* 写一方法,打印等长的二维数组,要求从1开始的自然数 ...
解答不错,有没有更好的算法啊。
作者:
kingwang
时间:
2015-11-8 15:04
这个题目很经典啊
作者:
为荣艺学iOS
时间:
2015-11-8 21:23
main()
{void build_array(int a[],int m,int n);
int a[15][15]={0};
int m,n;
printf("please input the fac nuuber of the uatrix m and n:(1~15):");
scanf("%d,%d",&m,&n);
build_array(a,m,n);
}
void build_array(int a[],int m,int n)
{int i,j,k=0,u=0;
do
{
for(j=u,i=u;i<m-u;i++)
{k++;
a[i*n+j]=k;
}
for(i=m-u-1,j=u+1;j<n-u;j++)
{k++;
a[i*n+j]=k;
}
for(j=n-u-1,i=m-u-2;i>=u;i--)
{k++;
a[i*n+j]=k;
}
for(i=u,j=m-u-2;j>u;j--)
{k++;
a[i*n+j]=k;
}
u++;
}while(u<=(m+n+1)/2);
printf("\nThe uatrix is:\n");
for(i=0;i<m;i++)
{for(j=0;j<n;j++)
printf("%4d",a[i*n+j]);
printf("\n");
}
}
作者:
Jared
时间:
2015-11-8 22:32
挺有意思的题啊
作者:
flyingwind
时间:
2015-11-10 01:06
import java.util.Scanner;
public class SpiralMatrix {
public static void main(String [] args){
Scanner scn= new Scanner(System.in);
System.out.println("请输入螺旋方阵的阶数: ");
int order=scn.nextInt();
SpiralMatrix sm=new SpiralMatrix();
sm.printClockWise(order);
}
//找到一个函数能够计算出order阶矩阵中 row 行, col列上的数值大小, 想到递归
public int getValue(int row, int col, int order) {
//基准条件
if(1==order) //最简单的一阶
return order;
if(0==order) //偶数阶的最简单形式
return order;
// 高阶矩阵而元素又位于矩阵的最外层, 能够通过简单的计算得出.
//由于涉及多个 return语句, 只用if即可 .
if(0==row)
return col+1;
if (col==order-1)
return col+row+1;
if(row==order-1)
return 3*order-2-col;
if(0==col)
return 4*order-3-row;
//所有简单的情况排除后,进行脱阶运算.
return (order*4-4)+getValue(row-1, col-1, order-2);
}
public void printClockWise(int order) {
int value;
for(int i=0;i<order; i++) {
for(int j=0;j<order; j++) {
value=getValue(i,j,order);
System.out.print(value+"\t");
}
System.out.println();
}
}
}
复制代码
作者:
jiao731097245
时间:
2015-11-10 11:11
/*
写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。 如: n = 4 则打印:
*/
class Test8
{
public static void main(String[] args)
{
printArray(10);
System.out.println("Hello World!");
}
public static void printArray(int len){
//定义递增
int val=1;
//定义二维数组
int[][] it=new int[len][len];
//定义循环次数,如果为偶数,则循环测试为长度的一般,如果为奇数,则为长度-1的一半
int count=len%2==0? len/2:(len-1)/2;
for(int x=0;x<count;x++){
//定义口子上面的数字顺序
for(int i=x,j=i;j<len-x;j++){
it[i][j]=val++;
}
//定义右面的数字顺序
for(int j=len-x-1,i=x+1;i<len-x;i++){
it[i][j]=val++;
}
//定义下面的数字顺序
for(int i=len-x-1,j=len-x-2;j>x-1;j--){
it[i][j]=val++;
}
//定义左面的数字顺序
for(int j=x,i=len-x-2;i>x;i--){
it[i][j]=val++;
}
}
if(len%2==1){
it[(len-1)/2][(len-1)/2]=val++;
}
for(int m=0;m<len;m++){
for(int n=0;n<len;n++){
System.out.print(it[m][n]+"\t");
}
System.out.println();
}
}
}
复制代码
作者:
sabrina妖儿
时间:
2015-11-10 19:35
学习一下
作者:
陌忆
时间:
2015-11-10 22:49
算法有很多种,反正第一行是打印1.。。。。n,n为奇数,最大数在n+1/2行n+1/2列,为偶数就在n/2+1行n/2列,最大数为n的平方,说白了就是以正方形遍历1.。。。。n*n找出规律总结出来
作者:
韩三少
时间:
2015-11-11 19:51
import java.util.Scanner;
public class Helix {
/**
* 螺旋输
*/
public static void main(String[] args) {
int size=5;
Scanner sc=new Scanner(System.in);
System.out.println("输入数组:");
size=sc.nextInt();
int count=0;
int [][]array=new int[size][size];
int m=0,n=0;
int down,right,up=0,left=0;
down=size-1;
right=size-1;
int max=size*size;
while(true){
count++;
if(m==up){
array[m][n]=count;
n++;
if(n>right){
n=right;
m++;
}
}
else if(n==right){
array[m][n]=count;
m++;
if(m>down){
m=down;
right--;
}
}
else if(m==down){
array[m][right]=count;
right--;
if(right<left){
right=left;
down--;
}
}
else if(right==left){
array[down][left]=count;
down--;
if(down<=up){
down=up;
left++;
}
}
else{
up++;
down=n-1;
right=m-1;
m=up;
n=left;
count--;
}
/////////////////////////////////////////////
if(count>=max){
break;
}
}// end loop
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
}
作者:
小龙2015
时间:
2015-11-16 18:21
import java.util.Scanner;
public class Helix {
/**
* 螺旋输出
*/
public static void main(String[] args) {
int size=5;
Scanner sc=new Scanner(System.in);
System.out.println("输入数组大小:");
size=sc.nextInt();
int count=0;
int [][]array=new int[size][size];
int m=0,n=0;
int down,right,up=0,left=0;
down=size-1;
right=size-1;
int max=size*size;
while(true){
count++;
if(m==up){
array[m][n]=count;
n++;
if(n>right){
n=right;
m++;
}
}
else if(n==right){
array[m][n]=count;
m++;
if(m>down){
m=down;
right--;
}
}
else if(m==down){
array[m][right]=count;
right--;
if(right<left){
right=left;
down--;
}
}
else if(right==left){
array[down][left]=count;
down--;
if(down<=up){
down=up;
left++;
}
}
else{
up++;
down=n-1;
right=m-1;
m=up;
n=left;
count--;
}
/////////////////////////////////////////////
if(count>=max){
break;
}
}// end loop
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
}
作者:
赖斯聪
时间:
2015-12-16 13:52
顶一个哈哈哈
作者:
sunpeijie
时间:
2015-12-20 14:39
我就看看 跟大神学习一下
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2