pat1023. Have Fun with Numbers (20)

1023. Have Fun with Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

 

 1 #include<set>
 2 #include<map>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<queue>
 8 #include<vector>
 9 #include<cmath>
10 using namespace std;
11 int num[25],dight[10];
12 int main(){
13     //freopen("D:\\input.txt","r",stdin);
14     string s;
15     cin>>s;
16     int i;
17     for(i=0;i<s.length();i++){
18         num[i]=s[s.length()-1-i]-'0';
19         
20         //cout<<num[i]<<endl;
21         
22         dight[num[i]]++;
23         num[i]*=2;
24         
25         //cout<<num[i]<<endl;
26     }
27     int k=0;
28     for(i=0;i<s.length();i++){
29         int sum=num[i]+k;
30         num[i]=sum%10;
31         k=sum/10;
32     }
33     
34     if(k){
35         num[i++]=k;
36     }
37     int len=--i;
38     for(;i>=0;i--){
39         dight[num[i]]--;
40     }
41     for(i=0;i<=9;i++){
42         if(dight[i]){
43             break;
44         }
45     }
46     
47     if(i==10){
48         printf("Yes\n");
49     }
50     else{
51         printf("No\n");
52     }
53     for(i=len;i>=0;i--){
54         cout<<num[i];
55     }
56     cout<<endl;
57     return 0;
58 }

 

posted @ 2015-08-28 00:56  Deribs4  阅读(283)  评论(0编辑  收藏  举报