1023 Have Fun with Numbers
题目
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 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:
1 | 1234567899 |
Sample Output:
1 | Yes |
题意
让你判断一个数,翻倍之后,是否还符合要求:
- 翻倍之后的每一个数字,都要是没翻倍之前出现过的.
- 翻倍之后的每一个数字,出现的频率都要和没翻倍之前一样.
思路
这一题最难的地方,是读懂题目,我就是因为没看明白原来还有频率要求,被卡好久了,只要明确了有这两个要求,就比较好办了.
另一点要注意的是,题目规定是20位之内的大数翻倍,所以不应该用整型长整型这些数据类型,而是应该用数组保存数据.
代码
既然要判断翻倍之后的数字是不是翻倍之前就存在的,那我们先创建一个用来判断某个数是不是在数组里面的函数:
1 | public static bool Contains(char[] input, char index) |
再创建一个函数,用来将整型数组翻倍:
1 | public static int[] DoubleIntegerArray(int[] num) |
然后创建一个函数用来判断频率是不是已经全为0了,用来判断翻倍之后的数字和翻倍之前频率是否一致
1 | public static bool FrequencyIsEmpty(int[] frequency) |
1 | public static void Main() |