标签:
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input file contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
Sample Input:1234567899Sample Output:
Yes 2469135798
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 public class number 6 { 7 public static void main(String[] args) throws IOException 8 { 9 BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); 10 String line=buf.readLine(); 11 char[] sou=line.toCharArray(); 12 char[] dou; 13 boolean b=false; 14 if(sou[0]-‘0‘>=5) 15 { 16 b=false; 17 dou=new char[sou.length+1]; 18 dou[0]=‘1‘; 19 for(int i=sou.length-1,jin=0;i>=0;i--) 20 { 21 dou[i+1]=(char) (((sou[i]-‘0‘)*2)%10+‘0‘+jin); 22 if((sou[i]-‘0‘)*2+jin>=10) 23 jin=1; 24 else 25 jin=0; 26 27 } 28 } 29 else 30 { 31 dou=new char[sou.length]; 32 for(int i=sou.length-1,jin=0;i>=0;i--) 33 { 34 35 dou[i]=(char) (((sou[i]-‘0‘)*2)%10+‘0‘+jin); 36 if((sou[i]-‘0‘)*2+jin>=10) 37 jin=1; 38 else 39 jin=0; 40 41 } 42 43 int[] t1=new int[10]; 44 for(int i=0;i<sou.length;i++) 45 { 46 t1[sou[i]-‘0‘]++; 47 } 48 int[] t2=new int[10]; 49 for(int i=0;i<dou.length;i++) 50 { 51 t2[dou[i]-‘0‘]++; 52 } 53 for(int i=0;i<t1.length;i++) 54 { 55 if(t1[i]!=t2[i]) 56 { 57 b=false; 58 break; 59 } 60 b=true; 61 } 62 } 63 64 if(b==true) 65 { 66 System.out.println("Yes"); 67 System.out.println(dou); 68 } 69 else 70 { 71 System.out.println("No"); 72 System.out.println(dou); 73 } 74 75 76 } 77 }
00-自测4. Have Fun with Numbers (20)
标签:
原文地址:http://www.cnblogs.com/qq1029579233/p/4398580.html