www.baidu.com

          JavaCode--我愛你,芳兒

          JavaStudy--我愛你,芳兒

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

          一個JAVA題目

          設(shè)計一個JAVA程序,打印N階螺旋矩陣:當(dāng)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+"個螺線型數(shù)組:");
           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;//奇數(shù)時i循環(huán)次數(shù)
          16                 }
          17                 else
          18                         intB = n/2;//偶數(shù)時i循環(huán)次數(shù)
          19                         
          20                 for(int i = 0;i < intB; i++){//從外到里循環(huán)
          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                 //輸出數(shù)組
          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 網(wǎng)民

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

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


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          公告

          芳兒寶貝.我愛你


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


           最近在讀的書:

          常用鏈接

          留言簿(1)

          隨筆分類(37)

          JavaCode

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 德保县| 滦平县| 湖北省| 法库县| 同心县| 德州市| 聊城市| 烟台市| 漳平市| 白城市| 吉木萨尔县| 鄂托克前旗| 页游| 扶风县| 柘荣县| 洞头县| 吉木萨尔县| 昌邑市| 裕民县| 武平县| 深圳市| 临西县| 密山市| 惠来县| 彭阳县| 敖汉旗| 远安县| 康乐县| 商洛市| 榆中县| 涞源县| 栾城县| 张北县| 房山区| 马关县| 密云县| 蓝田县| 怀远县| 德清县| 大城县| 潞西市|