A1023. Have Fun with Numbers

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<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<math.h>
 5 #include<string.h>
 6 using namespace std;
 7 char str[21];
 8 typedef struct info{
 9     int num[100];
10     int len;
11     info(){
12         for(int i = 0; i < 100; i++){
13             num[i] = 0;
14         }
15         len = 0;
16     }
17 }bign;
18 
19 bign a, c;
20 
21 bign add(bign a, bign b){
22     int carry = 0;
23     bign c;
24     for(int i = 0; i < a.len || i < b.len; i++){
25         int temp = a.num[i] + b.num[i] + carry;
26         c.num[i] = temp % 10;
27         carry = temp / 10;
28         c.len++;
29     }
30     if(carry != 0)
31         c.num[c.len++] = carry;
32     return c;
33 }
34 int main(){
35     int hashTB[10] = {0,0};
36     scanf("%s", str);
37     for(int i = strlen(str) - 1; i >= 0; i--){
38         a.num[a.len] = str[i] - '0';
39         hashTB[a.num[a.len]]++;
40         a.len++;
41     }
42     c = add(a, a);
43     for(int i = 0; i < c.len; i++){
44         hashTB[c.num[i]]--;
45     }
46     int tag = 0;
47     for(int i = 0; i < 10; i++){
48         if(hashTB[i] != 0){
49             tag = 1;
50             break;
51         }
52     }
53     if(tag == 0)
54         printf("Yes\n");
55     else printf("No\n");
56     for(int i = c.len - 1; i >= 0; i--){
57         printf("%d", c.num[i]);
58     }
59     cin >> str;
60     return 0;
61 }
View Code

 

总结:

1、大整数的记录结构:

  typedef struct info{
    int num[100];
    int len;
    info(){
    for(int i = 0; i < 100; i++){    //全初始化为0,这样在做加法时可以直接循环到最长的数,而不是仅仅循环到最短的数就结束。
      num[i] = 0;
    }
    len = 0;
    }
  }bign;

2、大整数的加法:

bign add(bign a, bign b){
    int carry = 0;
    bign c;
    for(int i = 0; i < a.len || i < b.len; i++){ //以长的数位界
        int temp = a.num[i] + b.num[i] + carry;
        c.num[i] = temp % 10;
        carry = temp / 10;
        c.len++;
    }
    if(carry != 0)
        c.num[c.len++] = carry;
    return c;
}

 

posted @ 2018-02-05 10:50  ZHUQW  阅读(192)  评论(0编辑  收藏  举报