题目描述
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.
翻译:注意数字123456789是一个9位数字并且完全由1-9个数字组成,没有重复。将它加倍我们会得到246913578,一个另外的9位数字,也完全由1-9个数字组成,只是排列不同。试着观察结果如果我们再将它加倍!
现在你需要检查是否有更多数字满足这种特性:将给出的数字加倍,你需要说出是否结果数字只由初始数字中的位数组成。
INPUT FORMAT
Each input file contains one test case. Each case contains one positive integer with no more than 20 digits.
翻译:每个输入文件包括一组测试数据。每组测试数据包括一个不超过20位的正整数。
OUTPUT FORMAT
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.
翻译:对于每组测试数据,首先输出一行“Yes”如果翻倍后仍然与一开始的数字只有排列上的不同,否则输出“No”。接下来一行输出翻倍后的数字。
Sample Input:
1234567899
Sample Output:
Yes
2469135798
解题思路
用字符数组保存数据,再转化为数字数组,注意倒序保存,不然进位会出错。然后用个计数数组判断每个数字出现的个数是否一样即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int a[25],v[10];
char s[25];
int main(){
scanf("%s",s);
int length=strlen(s);
for(int i=0;i<length;i++)
a[length-1-i]=s[i]-'0',v[a[length-1-i]]++;
for(int i=0;i<length;i++)a[i]*=2;
for(int i=0;i<length;i++){
if(a[i]>=10){
a[i+1]+=a[i]/10;
a[i]%=10;
}
v[a[i]]--;
}
int flag=0;
if(a[length]!=0)length++,flag=1;
else{
for(int i=0;i<9;i++){
if(v[i]){
flag=1;
break;
}
}
}
if(flag==0)printf("Yes\n");
else printf("No\n");
for(int i=length-1;i>=0;i--){
printf("%d",a[i]);
}
printf("\n");
return 0;
}