用java如何输出如下数组 1 2 6 7 3 5 8 13 4 9 12 14 10 11 15 16
8个回答
展开全部
我用C编过
#include<stdio.h>#include<stdlib.h>
int main()
{
int N,i,j,m;
int **a = NULL;
printf("please input N: ");
scanf("%d",&N);
a = (int **)malloc(N * sizeof(int *));
for(i = 0; i < N; i++)
a[i] = (int *)malloc(N * sizeof(int));
for(i = 0,j = 0,m = 1; m <= N * N; )
{
if(i == 0 && j == 0)
{
a[i][j++] = m++;
a[i++][j--] = m++;
}
else if(i == 0 || j == N - 1)
{
if(j != N - 1)
{
a[i][j++] = m++;
a[i][j] = m++;
while(j > 1)
a[++i][--j] = m++;
}
else
{
a[i++][j] = m++;
a[i][j] = m++;
while(i < N - 2)
a[++i][--j] = m++;
}
i++,j--;
}
else if(j == 0 || i == N - 1)
{
if(i != N - 1)
{
a[i++][j] = m++;
a[i][j] = m++;
while(i > 1)
a[--i][++j] = m++;
}
else
{
a[i][j++] = m++;
a[i][j] = m++;
while(j < N - 2)
a[--i][++j] = m++;
}
i--,j++;
}
}
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%-4d",a[i][j]);
printf("\n");
}
free(a);
return 0;
}
运行结果:
please input N: 8
1 2 6 7 15 16 28 29
3 5 8 14 17 27 30 43
4 9 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64
运行结果:
please input N: 4
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
你试着改编一下呗,我初学java
#include<stdio.h>#include<stdlib.h>
int main()
{
int N,i,j,m;
int **a = NULL;
printf("please input N: ");
scanf("%d",&N);
a = (int **)malloc(N * sizeof(int *));
for(i = 0; i < N; i++)
a[i] = (int *)malloc(N * sizeof(int));
for(i = 0,j = 0,m = 1; m <= N * N; )
{
if(i == 0 && j == 0)
{
a[i][j++] = m++;
a[i++][j--] = m++;
}
else if(i == 0 || j == N - 1)
{
if(j != N - 1)
{
a[i][j++] = m++;
a[i][j] = m++;
while(j > 1)
a[++i][--j] = m++;
}
else
{
a[i++][j] = m++;
a[i][j] = m++;
while(i < N - 2)
a[++i][--j] = m++;
}
i++,j--;
}
else if(j == 0 || i == N - 1)
{
if(i != N - 1)
{
a[i++][j] = m++;
a[i][j] = m++;
while(i > 1)
a[--i][++j] = m++;
}
else
{
a[i][j++] = m++;
a[i][j] = m++;
while(j < N - 2)
a[--i][++j] = m++;
}
i--,j++;
}
}
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%-4d",a[i][j]);
printf("\n");
}
free(a);
return 0;
}
运行结果:
please input N: 8
1 2 6 7 15 16 28 29
3 5 8 14 17 27 30 43
4 9 13 18 26 31 42 44
10 12 19 25 32 41 45 54
11 20 24 33 40 46 53 55
21 23 34 39 47 52 56 61
22 35 38 48 51 57 60 62
36 37 49 50 58 59 63 64
运行结果:
please input N: 4
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
你试着改编一下呗,我初学java
展开全部
public class Test {
public static void main(String[] args) {
int[][] a = new int[][]{{0,1,2,3,4}, {5,6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19}};
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
int[][] a = new int[][]{{0,1,2,3,4}, {5,6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19}};
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test {
public static void main(String[] args) {
int[][] a = new int[][]{{0,1,2,3,4}, {5,6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19}};
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
int[][] a = new int[][]{{0,1,2,3,4}, {5,6,7,8,9,10}, {11,12,13,14,15}, {16,17,18,19}};
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码如下:
int[] a={1 ,2 ,6 ,7 ,3 ,5 ,8 ,13 ,4 ,9 ,12 ,14 ,10 ,11 ,15 ,16};
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(a[i*4+j]+" ");
}
System.out.println();
}
int[] a={1 ,2 ,6 ,7 ,3 ,5 ,8 ,13 ,4 ,9 ,12 ,14 ,10 ,11 ,15 ,16};
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(a[i*4+j]+" ");
}
System.out.println();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有什么特点呢,就是给定1 2 6 7 3 5 8 13 4 9 12 14 10 11 15 16这个数组,然后作这样的输出么?如果是的我就给你写一个吧
int[] a={1 ,2 ,6 ,7 ,3 ,5 ,8 ,13 ,4 ,9 ,12 ,14 ,10 ,11 ,15 ,16};
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(a[i*4+j]+" ");
}
System.out.println();
}
int[] a={1 ,2 ,6 ,7 ,3 ,5 ,8 ,13 ,4 ,9 ,12 ,14 ,10 ,11 ,15 ,16};
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(a[i*4+j]+" ");
}
System.out.println();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询