A1023 Have Fun with Numbers (20 point(s))

大整数相乘

1. 原文

原题

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

1
1234567899

Sample output:

1
2
Yes
2469135798

2. 解析

大数乘法运算,从len-1位开始

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<cstdio>
#include<cstring>
char str[30];
int ans[30]={};
int exist[30]={};
int main(){
scanf("%s",str);
int len = strlen(str);
int idx = 0;
int carry = 0;
for (int i = len-1; i >= 0; i--)
{
int temp = (str[i]-'0')*2+carry;
ans[idx++]=temp%10;
carry = temp/10;
}
while(carry!=0){
ans[idx++]=carry%10;
carry/=10;
}
if (len!=idx)
{
printf("No\n");
}else{
for (int i = 0; i < len; ++i)
{
exist[str[i]-'0']++;
exist[ans[i]]--;
}
int flag = 0;
for (int i = 0; i < 10; ++i)
{
if (exist[i]>0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("Yes\n");
}else{
printf("No\n");
}

}
for (int i = idx-1; i >= 0; i--)
{
printf("%d",ans[i]);
}
printf("\n");


return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1023/
  • 发布时间: 2019-09-03 12:35
  • 更新时间: 2021-10-29 13:59
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!