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
判断一个数乘以2得到的数是否为原本的数的各位数的重新排列。这里输入字符串来处理会比较方便。将输入的字符串的每一位储存在的d1中,然后该位数乘以2加上上一位的进位数d,然后对10求余,结果储存在d2中(d2代表乘2得到的数),同时更新进位数d。这样就得到d1和d2。如果退出循环后d等于1,说明两个数位数都不一样,所以不符合。如果d不为1,将d1和d2排序然后对比每一位,如果有不相同即不符合,如果全部相同就是符合。
这里要注意的是不符合的时候,也要输出乘以2之后的数。
代码:
#include <iostream>
#include <cstring>
#include <set>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
void print(string s,vector<int>out)
{
cout<<s<<endl;
for(int i=out.size()-1;i>=0;i--)
{
cout<<out[i];
}
}
int main()
{
string num;
cin>>num;
vector<int>d1,d2;
int a[10];
memset(a,10,0);
int d=0;
for(int i=num.size()-1;i>=0;i--)
{
int n=num[i]-'0';
d1.push_back(n);
d2.push_back((n*2+d)%10);
d=(n*2+d)/10;
}
if(d==1)
{
d2.push_back(1);
print("No",d2);
return 0;
}
sort(d1.begin(),d1.end());
vector<int>out=d2;
sort(d2.begin(),d2.end());
for(int i=0;i<d1.size();i++)
{
if(d1[i]!=d2[i])
{
print("No",out);
return 0;
}
}
print("Yes",out);
}