www.baidu.com

JavaCode--我爱你,芳儿

JavaStudy--我爱你,芳儿

一个JAVA题目

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&lt;n;i++)
18        {
19            for(int j=0;j&lt;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&lt;=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ǒ愛伱--咾婆 阅读(3409) 评论(1)  编辑  收藏 所属分类: JavaQuestionNeedToSolve

评论

#帮助一下 2013-09-28 13:17 网民

C[i][j]=A[i][0]*B[0][j]+A[i][1]*B[1][j]+...+A[i][k]*B[k][j];
回复  更多评论

 只有注册用户登录后才能发表评论。 网站导航: 相关文章:

• 随笔 - 73
• 文章 - 1
• 评论 - 44
• 引用 - 0

公告

http://www.hackbase.com

最近在读的书：

•

• 积分 - 90461
• 排名 - 628