1023 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:

1234567899

Sample Output:

Yes
2469135798

注意点:输出正确的判断应该是carry!=0&&flag
题目大意: 给出一个长度再20以内的数字字符串, 将其 乘以2, 判断结果中的数字个数是不是和原来一样

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 using namespace std;
 5 int main(){
 6   string s, s1;
 7   vector<int> v(10, 0);
 8   cin>>s;
 9   s1 = s;
10   int carry=0, i;
11   for(i=s.size()-1; i>=0; i--){
12     //记录0-9中哪些数字出现,以出现次数
13     v[s1[i]-'0']++;
14     //模拟乘以2的操作
15     s[i] += (s[i]-'0' + carry);
16     if(s[i]>'9'){
17       s[i] -= 10;
18       carry = 1;
19     }else carry = 0;
20   }
21   bool flag=true;
22   if(carry==1) printf("NO
%d%s", carry, s.str());
23   else{  
24     for(i=0; i<s.size(); i++){
25     if(v[s[i]-'0']>0){
26       v[s[i]-'0']--;
27     }else{
28       printf("NO
%s", s.c_str());
29       flag = false;
30       break;
31     }
32   }
33   }
34   if(flag&&carry!=1) cout<<"Yes
"<<s;
35   return 0;
36 }
有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
原文地址:https://www.cnblogs.com/mr-stn/p/9178447.html