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 :
1234567899
Sample Output :
Yes
2469135798
思路:
最高位不出现进位,并且每个数字出现的次数和原有数字出现次数相同,则打印Yes,否则打印No。十分注意,最高位出现进位时要在最高位前面加上一个数字1!!!
代码如下
#include<iostream>
#include<cstring>
using namespace std;
int digit[10];//存储各个原有数字出现的次数
int main()
{
char num[25];int up = 0;//up表示进位
scanf("%s", num);
for(int i = strlen(num)-1; i >= 0; i--){
int tmp = num[i]-'0';
digit[tmp]++;
tmp = tmp*2 + up;
up = 0;//进位置0
if(tmp >= 10){
tmp -= 10;
up = 1;//进位置1
}
num[i] = tmp + '0';
digit[tmp]--;
}
int flag = 0;
for(int i = 0; i < 10; i++){
if(digit[i] != 0) flag = 1;
}
printf("%s", (up==1 || flag==1)?"No\n":"Yes\n");
if(up == 1) printf("1");
printf("%s", num);
return 0;
}