Saturday 15 February 2014

java - Returning Next Permutation of a number -



java - Returning Next Permutation of a number -

the problem stated here. wrote code accomplish task.

/** * created aditya on 16-10-2014. */ import java.io.bufferedinputstream; import java.util.arraylist; import java.util.scanner; public class main { public static void main(string args[]) { scanner scan = new scanner(new bufferedinputstream(system.in)); int n = scan.nextint(); int k = scan.nextint(); arraylist<integer> list = new arraylist<integer>(k); arraylist<integer> list_out = new arraylist<integer>(k); (int = 0; < k; i++) { list.add(scan.nextint()); } (int j = 0; j < k; j++) { list_out.add(next_perm(list.get(j), n)); } (int temp : list_out) { system.out.println(temp); } system.out.flush(); } public static int next_perm(int k, int n) { int perm = 0; int number = k; int num[] = new int[n]; (int = 0; < n; i++) { num[i] = number % 10; number /= 10; } int k_max = 0; (int = 0; < n; i++) { if (i == (n-1)) break; if (num[i + 1] >= num[i]) { ++k_max; } else if (num[i + 1] < num[i]) { break; } } if(k_max==(n-1))return k; int j_max = k_max+1; (int = k_max; >= 0; i--) { if (num[i] > num[k_max + 1]) { --j_max; } else { break; } } int temp=num[j_max]; num[j_max]=num[k_max+1]; num[k_max+1]=temp; for(int i=k_max,j=0;i>j;i--,j++){ int tempo = num[i]; num[i]=num[j]; num[j]=tempo; } (int = 0; < n; i++) { perm += num[i] * (int) math.pow(10, i); } homecoming perm; } }

and manually ran permutations of 1,2,..n n=1 4 , got right results. of wondering logic see here. no warnings or errors displayed. when submitted online justice declares wrong reply (after conducting 9 tests). [see below link , results] 9 tests must subset of permutations of n=1 9 corresponding each test @ to the lowest degree 4 tests should correct, code passes no tests, please help me see no bugs.

edit: see sample input-output case of n=4 here.added below too

note: converted "\n" "space" space issues in question

(input) 4 24 1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321

(output) 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 4321

edit: see results here. added here too:

problem: nextperm state: wrong answer total score submission: 0

test case #0 2 points wrong reply runtime: 0.12

test case #1 2 points wrong reply runtime: 0.124

test case #2 2 points wrong reply runtime: 0.124

test case #3 2 points wrong reply runtime: 0.12

test case #4 2 points wrong reply runtime: 0.116

test case #5 2 points wrong reply runtime: 0.124

test case #6 2 points wrong reply runtime: 0.124

test case #7 2 points wrong reply runtime: 0.14

test case #8 2 points wrong reply runtime: 0.108

test case #9 2 points wrong reply runtime: 0.124

apparently worked with:

import java.util.arraylist; import java.util.collections; import java.util.scanner; /** * created aditya on 14-10-2014. */ public class main { public static void main(string args[]) { scanner scan = new scanner(system.in); int n = integer.parseint(scan.nextline()); arraylist<string> lines = new arraylist<string>(); arraylist<string> words = new arraylist<string>(); arraylist<string> words_2 = new arraylist<string>(); boolean once_entered = true; (int = 0; < n; i++) { lines.add(i, scan.nextline() + " "); } (int = 0; < n; i++) { string word = ""; (int j = 0; j < lines.get(i).length(); j++) { char char_0 = lines.get(i).tolowercase().charat(j); if ((int) (char_0) >= (int) ('a') && (int) (char_0) <= (int) ('z')) { word += char_0; once_entered = false; } else if (!once_entered) { words.add(word); word = ""; once_entered = true; } } } (int = 0; < words.size(); i++) { boolean contains =false; for(int j=0;j<words_2.size();j++){ if(words_2.get(j).contentequals(words.get(i))) contains=true; } if(!contains) words_2.add(words.get(i)); } collections.sort(words_2); system.out.println(words_2.size()); (int = 0; < words_2.size(); i++) { system.out.println(words_2.get(i)); } } }

seems there problem duplicates , minor ones.

java permutation

No comments:

Post a Comment