saif Active member
| Subject: SPIRAL matrix Program using My logic Wed Feb 24, 2010 12:49 am | |
| 7 1 2 3 4 5 6 7 24 25 26 27 28 29 8 23 40 41 42 43 30 9 22 39 48 49 44 31 10 21 38 47 46 45 32 11 20 37 36 35 34 33 12 19 18 17 16 15 14 13
But I am sure that it won't be asked tomorrow
import java.io.*; class spiral {
private static int arr[][]; private static int c=-1,r=0,count=0; public static void main(String args[])throws IOException { BufferedReader obj =new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(obj.readLine()); arr=new int[n][n]; fill('R',n);
for(int i=n-1;i>=1;i--) { fill('D',i); fill('L',i); if(--i==0)break; fill('U',i); fill('R',i); }
for(int i=0;i |
|
WinRrule Active member
| Subject: Re: SPIRAL matrix Program using My logic Fri Mar 12, 2010 5:30 pm | |
| ur expectation was right..! |
|
RC Active member
| Subject: Re: SPIRAL matrix Program using My logic Sat Mar 13, 2010 11:54 am | |
| class S1002 { void main(int n) { int a[][]=new int[n][n];int c=1; for (int i = n-1, j = 0; i > 0; i--, j++) { for (int k = j; k < i; k++) a[j][k]=c++; for (int k = j; k < i; k++) a[k][i]=c++; for (int k = i; k > j; k--) a[i][k]=c++; for (int k = i; k > j; k--) a[k][j]=c++; } //special case for middle element if N is odd if (n % 2 == 1) a[(n-1)/2][(n-1)/2]=c++; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) System.out.print(a[i][j]+" "); System.out.println();} }
} |
|
Sponsored content
| Subject: Re: SPIRAL matrix Program using My logic | |
|
|
|