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的数字组成,只是在不同的排列中。如果我们再次加倍,请检查结果!
现在您要检查此属性是否有更多数字。也就是说,给定数字加倍k个数字,您要判断结果数字是否仅包含原始数字中数字的排列。
二倍后是否为原来数字每个数字的一个排列?
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
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int book[10];
int main()
{
char num[22];
cin>>num;///输入字符形式的大整数
int flag=0,len=strlen(num);
for(int i=len-1;i>=0;i--)
{
int temp=num[i]-'0';///从最后一位开始变成数字
book[temp]++;
temp=temp*2+flag;///本位乘2加低位的进位
flag=0;///没有进位
if(temp>=10)
{///本位乘2后进位
temp=temp-10;
flag=1;///向高位进1,不可能进2(9*2+1=19)
}
num[i]=(temp+'0');///重新变成字符形式
book[temp]--;///通过判断存储原数的数组能否被全部清0来判断是否为原数的排列
}
int flag1=0;
for(int i=0;i<10;i++)
{
if(book[i]!=0)
flag1=1;///有不是0的说明不是排列
}
printf("%s",(flag==1||flag1==1)?"No\n":"Yes\n");///多出一位一定不匹配!
if(flag==1)cout<<"1";///一个特例???
cout<<num;///字符串
}
学到了大整数的运算,需要定义一个进位数flag,进位后要归零,数位大于10要向高位进位