1023. Have Fun with Numbers
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1023. Have Fun with Numbers相关的知识,希望对你有一定的参考价值。
1023. Have Fun with Numbers (20)
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
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int init[]={0,0,0,0,0,0,0,0,0,0}; String s=in.nextLine(); int length = s.length(); int[] str1 = new int[length]; for(int i = 0; i < length; i++) { str1[i] = s.charAt(i) - ‘0‘; } int[] str2 =new int[21]; int add=0,i=0, num=0,tmp=0; for (int k:str1){ init[k]++; } for(int g=str1.length-1;g>=0;g--){ tmp=str1[g]; num=tmp*2+add; if(num>=10){ str2[i++]=num%10; add=num/10; } else{ str2[i++]=num; add=0; } } if(add!=0){ str2[i++]=add; } for(int k=i-1;k>=0;k--){ init[str2[k]]--; } int flag=0; for (int k=0;k<10;k++){ if(init[k]!=0) { flag=1; break; } } if(flag==1) System.out.println("No"); else System.out.println("Yes"); for (int k=i-1;k>=0;k--){ System.out.print(str2[k]); } System.out.println(); } }
这题要注意的是:
1.最高位如果有进位不要漏掉;
2.字符串的处理吧,之前写的代码用的split一直通过不了,不知道为啥在同学的机子上跑出来的话最高位会缺失,但是我自己的就正常可能是这里有些问题吧,不懂,希望之后能想明白orz
错误的代码如下
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int init[]={0,0,0,0,0,0,0,0,0,0}; String s=in.nextLine(); String str1[]=s.split(""); int[] str2 =new int[21]; int add=0,i=0, num=0,tmp=0; for (String k:str1){ init[Integer.parseInt(k)]++; } for(int g=str1.length-1;g>=0;g--){ tmp=Integer.parseInt(str1[g]); num=tmp*2+add; if(num>=10){ str2[i++]=num%10; add=num/10; } else{ str2[i++]=num; add=0; } } if(add!=0){ str2[i++]=add; } for(int k=i-1;k>=0;k--){ init[str2[k]]--; } int flag=0; for (int k=0;k<10;k++){ if(init[k]!=0) { flag=1; break; } } if(flag==1) System.out.println("No"); else System.out.println("Yes"); for (int k=i-1;k>=0;k--){ System.out.print(str2[k]); } System.out.println(); } }
以上是关于1023. Have Fun with Numbers的主要内容,如果未能解决你的问题,请参考以下文章
PAT 1023 Have Fun with Numbers
PAT 甲级 1023 Have Fun with Numbers
PAT1023. Have Fun with Numbers (20)