/* * this class solve the problem of "Hide that Number" * Briefly: given a number 123, crypt it as follows: * 123 * 10 + 123 = 1353, cut the any extra leading digit * so 123 --> 353 * This program has the: * hide method: to hide/encrypt number * unhid method: to unhide the number if possoble. */ public class HideThatNumber { public static void main(String[] agrs){ for(int i=1;i<10000;i++){ String hidden = i+""; String t2 = unhide( hidden ); System.out.println( hidden + " -->" + t2 + "==>" + ( (hidden.length()!= t2.length()) ? "Imposible":"")); } String num = "1234567890123456789001234567890"; System.out.println("hiding: " + num); System.out.println(hide(num)+ "-->" + unhide(hide(num))); num = "123456"; System.out.println(unhide(num)); } // hide method that does not consider large integers. public static int hide(int n){ int m = n * 10; m = m + n; int ll = len(m) - len(n); if( ll == 0) return m; else{ return Integer.parseInt( (m +"").substring(1)); } } /* this method hides numbers in the format of string * That is because integer format is limited and cannot hold * big number of too many digits. */ public static String hide(String num){ StringBuffer sb = new StringBuffer(num); StringBuffer orig = new StringBuffer(num); orig.insert(0,'0'); StringBuffer result = new StringBuffer(); sb.append('0'); int c = 0; for(int i=orig.length()-1;i>=0;i--){ int d1 = sb.charAt(i) - '0'; int d2 = orig.charAt(i) - '0'; int res = d1 + d2 + c; //System.out.println(d1 + "+" + d2 + "+ "+ c +" =" + res); if(res >9){ int digit = res % 10; c = res / 10; result.insert(0,digit); }else{ result.insert(0,res); c=0; } } if(c != 0) result.insert(0,c); //cut extra digits more than the original int l = result.length() - orig.length() + 1; result = new StringBuffer(result.substring(l)); //remove any leading zeros... for(int i=0;i" + (x/11) ); flag = true; } } } if(!flag) System.out.println(n +"--> Impossible"); } public static int len(int n){ int m = 0; while( n > 0) { m++; n/=10; } return m; } public static int putLeft(int n, int rm){ String s = n +""; int v = 1; for(int i=0;i=0;i--){ int dif = (num.charAt(i) - '0') - curHidden; if(borrow == 1) dif--; if(dif < 0) { sb.insert(0, dif+10); borrow = 1; curHidden = dif+10; }else if( dif >= 0){ sb.insert(0,dif); curHidden = dif; if(borrow == 1) borrow = 0; } } //remove zeros... for(int i=0;i