www.baidu.com

          JavaCode--我愛你,芳兒

          JavaStudy--我愛你,芳兒

          設計一個JAVA程序,打印N階螺旋矩陣

          一個JAVA題目

          設計一個JAVA程序,打印N階螺旋矩陣:當N=3時,則
          1 2 3
          8 9 4
          7 6 5


           1public class Ring
           2{
           3    public static int getNum(int n,int i,int j)
           4    {
           5        if(i==0return j+1;
           6        else if(i==n-1
           7        return 3*n-j-2;
           8        else if(j==0
           9        return 4*n-i-3;
          10        else if(j==n-1
          11        return n+i;
          12        else 
          13        return 4*(n-1)+getNum(n-2,i-1,j-1);
          14    }

          15    public static void print(int n)
          16    {
          17        for(int i=0;i<n;i++)
          18        {
          19            for(int j=0;j<n;j++
          20            System.out.printf("%7d",getNum(n,i,j));
          21            System.out.println();
          22        }

          23    }

          24    public static void main(String[] args)
          25    {
          26        Scanner scan =new Scanner(System.in);
          27        while(true)
          28        {
          29            System.out.println("input a integer(0 to exit):");
          30            int n =scan.nextInt();
          31            if(n<=0break;
          32            print(n);
          33        }
           
          34     }

          35}

           1 import java.io.*;
           2 
           3 public class RingDemo{
           4         
           5         public void setArray(int n){
           6                 
           7                 System.out.println("這是"+n+"個螺線型數組:");
           8         
           9                 int intA=1;//初始化
          10                 int[][] array=new int[n][n];
          11                 
          12                 int intB;
          13                 
          14                 if(n%2!=0){
          15                         intB = n/2+1;//奇數時i循環次數
          16                 }
          17                 else
          18                         intB = n/2;//偶數時i循環次數
          19                         
          20                 for(int i = 0;i < intB; i++){//從外到里循環
          21                         //從左到右橫的開始
          22                         for(int j=i;j < n-i; j++){
          23                                 array[i][j] = intA;
          24                                 intA++
          25                         }
          26                         //從上到下縱
          27                         for(int k=i+1;k < n-i; k++){
          28                                 array[k][n-i-1= intA;
          29                                 intA++;
          30                         }
          31                         //從右到左橫
          32                         for(int l=n-i-2;l >= i ;l--){
          33                                 array[n-i-1][l] = intA;
          34                                 intA++;
          35                         }
          36                         //從下到上縱
          37                         for(int m=n-i-2; m > i ; m--){
          38                                 array[m][i]=intA;
          39                                 intA++;
          40                         }
          41                 }
          42                 //輸出數組
          43                 for(int i = 0; i < n; i++){
          44                         for(int j = 0; j < n; j++){
          45                                 System.out.print(array[i][j] + " ");
          46                         }
          47                         System.out.println();
          48                 }
          49         }
          50         
          51         public static void main(String[] args){
          52                 String strIn = "";
          53                 System.out.print("please enter a number:");
          54                 InputStreamReader input = new InputStreamReader(System.in);
          55                 BufferedReader buff=new BufferedReader(input);
          56                 try{
          57                          strIn=buff.readLine();
          58                 }
          59                 catch(IOException e){
          60                         System.out.println(e.toString());
          61                 }
          62                 
          63                 int int1 = Integer.parseInt(strIn);
          64                 RingDemo ring = new RingDemo();
          65                 ring.setArray(int1);
          66         }
          67 }


           

          芳兒寶貝.我愛你

          posted on 2007-12-04 23:42 wǒ愛伱--咾婆 閱讀(3461) 評論(1)  編輯  收藏 所屬分類: JavaQuestionNeedToSolve

          評論

          # 幫助一下 2013-09-28 13:17 網民

          編寫一個程序,計算3x4矩陣A與4x5矩陣B的乘積并打印(屏幕顯示)出來,矩陣請用二維數組存貯.

          C[i][j]=A[i][0]*B[0][j]+A[i][1]*B[1][j]+...+A[i][k]*B[k][j];
            回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          芳兒寶貝.我愛你


          黑客基地
          http://www.hackbase.com
          全球最大的黑客門戶網站


           最近在讀的書:

          常用鏈接

          留言簿(1)

          隨筆分類(37)

          JavaCode

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 白河县| 彩票| 邢台市| 临夏市| 石狮市| 灵寿县| 贡嘎县| 江门市| 政和县| 荥阳市| 汶上县| 交城县| 北京市| 衡阳市| 定陶县| 如皋市| 原平市| 宣汉县| 化州市| 石家庄市| 榆林市| 石狮市| 兴宁市| 察哈| 建宁县| 兴义市| 米林县| 辽宁省| 鄱阳县| 资兴市| 株洲市| 巴塘县| 遂溪县| 桂平市| 南城县| 莒南县| 兴和县| 威宁| 长顺县| 郓城县| 怀仁县|