题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4
题面:
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
题目大意:
就是问给定的一个数字乘以2之后,得到的新数字包括的每一个数字的个数是否与原数字包括的数量同样。题意是比較绕的。说什么乘二又乘二,又什么1-9。事实上。就是仅仅乘了1遍2。数字也能够包括0。数据是比較水的。这也是PAT比不上ACM的地方。网上一组仅仅数1-9的也过了。
解题:
由于数字有20位,long long也不够用。所以就自己手动模拟一下乘2的过程吧。
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <string> using namespace std; int main() { //计数。存储 int cnt1[10],cnt2[10],num1[25],num2[25],p=0,f=0,x; string s; bool flag=true; memset(cnt1,0,sizeof(cnt1)); memset(cnt2,0,sizeof(cnt2)); memset(num1,0,sizeof(num1)); memset(num2,0,sizeof(num2)); //输入 cin>>s; for(int i=s.length()-1;i>=0;i--) { num1[p++]=s[i]-'0'; } //模拟乘2 for(int i=0;i<25;i++) { x=num1[i]*2+f; num2[i]=x%10; f=x/10; } //计数 for(int i=0;i<s.length();i++) { cnt1[num1[i]]++; cnt2[num2[i]]++; } //特判最高位 if(num2[s.length()])cnt2[s.length()]++; for(int i=0;i<10;i++) if(cnt1[i]!=cnt2[i]) { flag=false; break; } //输出 if(flag)cout<<"Yes\n"; else cout<<"No\n"; flag=false; //假设是0,那么p没有被赋值,输出0 p=0; //去前导0 for(int i=24;i>=0;i--) { if(num2[i]) { p=i; break; } } //输出 for(int i=p;i>=0;i--) cout<<num2[i]; cout<<endl; return 0; }