Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
can anyone tell me whats wrong???Im try to solve this problem, i understand it in that way: if we have n numbers, then diference of two adjacents numbers must be 1<=dif<=n-1, thats right??? if thats true, then if we have n = 10, then the difences must be :1,2,3,4,5,6,7,8,9 almost once, thats right??? If anyone can and wants to help me please let me know, here is the code: import java.math.*; import java.util.*; import java.io.*; class Main { public static void main(String ar[]) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { // BufferedReader in = new BufferedReader(new FileReader("in.txt")); String cs=""; while((cs=in.readLine())!=null) { StringTokenizer str =new StringTokenizer(cs); int n = new Integer(str.nextToken());/*We get the n value*/ boolean jolly=true; int nmbrs[]=new int[n+1]; boolean difs[]=new boolean[n+1]; int i = 1; while((str.hasMoreTokens()))/*We save all numbers in an array*/ nmbrs[i++]=new Integer(str.nextToken()); int j = 0; for(i=1;i+1<=n;i++)/*now we move trough all pairs in the array*/ { int dif=Math.abs(nmbrs[i]-nmbrs[i+1]);/*the absolute value of the diference*/ //System.out.print(dif+" "); if(dif>=1&&dif<=n-1)/*it is valid*/ difs[dif]=true; } //System.out.println(); for(i=1;i<=n;i++)/*now we count how many dif are valid, if all of there are valid, then there is a jolly secuence*/ j=difs[i]?j+1:j; System.out.println(j==n-1?"Jolly":"Not Jolly"); } } catch(Exception e){e.printStackTrace();}; new Main(); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator