00-自测4. Have Fun with Numbers (20)
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 file 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:1234567899Sample Output:
Yes 2469135798
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int c[10],c1[10]; //用于存放0~9出现的次数,c0是村输入的,c1是存加倍后的
int main()
{
int i,j=0,sum=0,jinwei=0,flag=0;
int c2[21]; //20位数的数加倍会出现21位数的情况
string str;
string::iterator it;
stack<int> s;
cin>>str;
for(it=str.begin();it!=str.end();it++) //字符串入栈,反序输出,便于逐个double加倍
s.push((*it)-'0');
while(!s.empty())
{
int a=s.top();
c[a]++;
sum=2*a+jinwei; //字符加倍,考虑进位
if(sum<10){
c1[sum]++;
c2[j++]=sum; //逐个存放加倍后数据
jinwei=0;
}
else{
c1[sum%10]++;
c2[j++]=sum%10;
jinwei=1;
}
s.pop();
if(s.empty() && jinwei==1) //最高位有进位情况
c2[j++]=1;
}
for(i=0;i<10;i++){
if(c[i]!=c1[i]) //比较
flag=1;
}
if(flag==1)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
for(int t=j-1;t>=0;t--){ //反序输出
cout<<c2[t];
}
cout<<endl;
return 0;
}