We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.util.*;publicclassSolution{// public static void main(String[] args) {// /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */// }staticlong[]fact=newlong[14];publicstaticvoidmain(String[]args){factorialFill();Scannerscanner=newScanner(System.in);intt=scanner.nextInt();for(inta0=0;a0<t;a0++){longn=scanner.nextLong();int[]arrc=newint[13];factorialNum(n-1,arrc);printPermutation(arrc);System.out.println();}scanner.close();}staticvoidfactorialFill(){fact[0]=1;for(inti=1;i<14;i++){fact[i]=i*fact[i-1];}}staticvoidfactorialNum(longn,int[]arrc){longtmp=n;intindex=0;for(inti=13;i>=0;i--){if(fact[i]<=tmp){index=i;break;}}longq,r;while(index>=0){q=tmp/fact[index];r=tmp%fact[index];arrc[index]=(int)q;tmp=r;index--;}}staticvoidupdateList(char[]str,intindex){for(inti=index+1;i<=12;i++){str[i-1]=str[i];}}staticvoidprintPermutation(int[]arrc){char[]str="abcdefghijklm".toCharArray();for(inti=12;i>=0;i--){System.out.print(str[arrc[i]]);updateList(str,arrc[i]);}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #24: Lexicographic permutations
You are viewing a single comment's thread. Return to all comments →
JAva code