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.
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
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.
1234567899
Yes
2469135798
这道题考察了大数处理,遇到大数,首选Java或者Python
Java
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean right = true; BigInteger b1, b2; b1 = sc.nextBigInteger(); b2 = b1.multiply(new BigInteger("2")); int[] a = new int[10]; int[] b = new int[10]; String s1 = b1.toString(); String s2 = b2.toString(); for(int i=0;i<s1.length();i++) a[s1.charAt(i)-‘0‘]++; for(int i=0;i<s2.length();i++) b[s2.charAt(i)-‘0‘]++; for(int i=0;i<10;i++) if(a[i]!=b[i]) right = false; if(right) System.out.println("Yes"); else System.out.println("No"); System.out.println(s2); } }
Python
a = int(input()) b = a*2 arr = [] arr2 = [] for i in range(10): arr.append(0) arr2.append(0) for x in str(a): index = int(x) arr[index] = arr[index]+1 for x in str(b): index = int(x) arr2[index] = arr2[index]+1 right = True for i in range(10): if(arr[i] != arr2[i]): right = False if(right): print("Yes") else: print("No") print(b)
PAT Advanced 1023 Have Fun with Numbers (20分)
原文:https://www.cnblogs.com/littlepage/p/12220165.html