BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #7912同步于 2006/5/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

问一道课后习题[不是作业贴]

CanFly
2006/5/20镜像同步4 回复
题意 输出下列方阵: 当n=4时 1 2 6 7 3 5 8 13 4 9 12 14 10 11 15 16 当n=3时 1 2 6 3 5 7 4 8 9 当n=i时..以此类推了.... 总之就是把1到n*n这些数按照一斜列一斜列的方式首尾相接打印出来..... 呃..谢谢了......想了很久都不会~~ 最好是java版的方法,呵呵,因为就是java书后的习题~
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
Nonsense机器人#1 · 2006/5/22
{*******************************************************} { 在 Delphi 7.0 下编译通过 } {*******************************************************} program Nonsense; {$APPTYPE CONSOLE} uses SysUtils; var n:Integer;//矩阵行列数 i,j:Integer;//横纵座标 myArray:array of array of Integer; begin { TODO -oUser -cConsole Main : Insert code here } //取得N的值 Write('请输入矩阵的行列数:'); Read(n); //输入行列数 Writeln('您输入的数字为:'+inttostr(n)); //设置动态数组为N*N矩阵 SetLength(myArray,n,n); //*************************二维数组生成部分**************************************// //两个循环 for i:=1 to n do for j:=1 to n do begin //myArray[i-1,j-1]:=i*10+j; if (i+j)<=(n+1) then begin //上三角部分通式 myArray[i-1,j-1]:=((i+j-1) mod 2)*( ((i+j-1)*(i+j-2)div 2) + j ) + ((i+j) mod 2)*( ((i+j-1)*(i+j-2)div 2) + i ); end else begin //下三角部分通式 myArray[i-1,j-1]:=((i+j-1) mod 2) * ((n*(n+1) div 2) + ((3*n+1-i-j)*(i+j-n-2) div 2) + (n-i+1)) + ((i+j) mod 2) * ((n*(n+1) div 2) + ((3*n+1-i-j)*(i+j-n-2) div 2) + (n-j+1)); end; end; //没有化简,基本是原始式子,所以很长~很长~很长~很长~~,肯定多占了cpu 不少时钟周期 ^_^ //*************************************************************************// //将二维数组myArray打印输出 for i:=1 to n do begin for j:=1 to n do begin Write(inttostr(myArray[i-1,j-1])+' '); end; Writeln; Writeln; end; end.
coolfantasy机器人#2 · 2006/5/22
//C++ Code //Version:1.0.0.0 beta //Author:Cool #include<iostream> #include<math.h> using namespace std; int main(void) { const int N = 10; int m = 0,n = 0,line,total_line,i,total_i,count = 1,k,j,a[N][N]; total_line = 2 * N - 1; a[0][0] = 1; for(line = 0;line <= total_line;line++) { total_i = N - abs(line - N + 1); for(i = 1;i <= total_i;i++) { if(count == N * N) break; if(i != total_i) { if(line % 2 == 0) { m--; n++; } else { m++; n--; } } else { if(line < N - 1) { if(line % 2 == 0) { n++; } else { m++; } } else { if(line % 2 == 0) { m++; } else { n++; } } } a[m][n] = ++count; } } for(k = 0;k < N;k++) { for(j = 0;j < N;j++) { printf("%4d",a[k][j]); } cout << endl; } cin >> i; //to make the screen stay return 0; }
boots机器人#3 · 2006/5/22
public class test { public static void main(String[] args) { final int NUM = 10; int[][] a = new int[NUM][NUM]; // init for (int i = 0; i < NUM; i++) { for (int j = 0; j < NUM; j++) a[i][j] = 0; } // print for (int i = 0; i < NUM; i++) { for (int j = 0; j < NUM; j++) if (j == NUM - 1) System.out.println(a[i][j]); else System.out.print(a[i][j] + " "); } // 赋值 int n = 1; // 左上三角包括中间 for (int i = 0; i < NUM; i++) { for (int j = 0; j <= i; j++) if (i % 2 == 0) a[i - j][j] = n++; else a[j][i - j] = n++; } // 右下三角 int m = 1; for (int i = NUM; i < NUM + NUM - 1; i++) { for (int j = m; j < NUM; j++) if (i % 2 == 0) a[i - j][j] = n++; else a[j][i - j] = n++; m++; } // print System.out.println("***********************************"); for (int i = 0; i < NUM; i++) { for (int j = 0; j < NUM; j++) if (j == NUM - 1) System.out.println(a[i][j]); else System.out.print(a[i][j] + " "); } } } 【 在 CanFly (幸福姐的粉丝 CanFly||人生八苦的五阴盛) 的大作中提到: 】 : 呵呵,我大概知道思路了,谢谢了~
Xhan机器人#4 · 2006/5/23
我也帖一个,请指教,呵呵 #include <iostream.h> #define MAXROW 3 int main(int argc, char* argv[]) { int matrix[MAXROW][MAXROW]; bool direct=true;//true right false left int i=0; int j=0; int count=1; while((i<MAXROW) && (j<MAXROW)) { matrix[i][j]=count++; if(((i==0)||(j==MAXROW-1))&&(direct)) { if(j==MAXROW-1) i++; else j++; direct=!direct; continue; } if(((j==0)||(i==MAXROW-1))&&(!direct)) { if(i==MAXROW-1) j++; else i++; direct = !direct; continue; } if(direct) { i--; j++; } else { i++; j--; } } for(i=0; i <MAXROW; i++) { for(int j=0; j<MAXROW; j++) { cout<<matrix[i][j]<<'\t'; } cout<<endl; } return 0; }