A1023. Have Fun with Numbers

时间:2022-03-06 17:53:42

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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
char str[];
typedef struct info{
int num[];
int len;
info(){
for(int i = ; i < ; i++){
num[i] = ;
}
len = ;
}
}bign; bign a, c; bign add(bign a, bign b){
int carry = ;
bign c;
for(int i = ; i < a.len || i < b.len; i++){
int temp = a.num[i] + b.num[i] + carry;
c.num[i] = temp % ;
carry = temp / ;
c.len++;
}
if(carry != )
c.num[c.len++] = carry;
return c;
}
int main(){
int hashTB[] = {,};
scanf("%s", str);
for(int i = strlen(str) - ; i >= ; i--){
a.num[a.len] = str[i] - '';
hashTB[a.num[a.len]]++;
a.len++;
}
c = add(a, a);
for(int i = ; i < c.len; i++){
hashTB[c.num[i]]--;
}
int tag = ;
for(int i = ; i < ; i++){
if(hashTB[i] != ){
tag = ;
break;
}
}
if(tag == )
printf("Yes\n");
else printf("No\n");
for(int i = c.len - ; i >= ; i--){
printf("%d", c.num[i]);
}
cin >> str;
return ;
}

总结:

1、大整数的记录结构:

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

2、大整数的加法:

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