当前位置: 代码迷 >> 综合 >> Hide That Number
  详细解决方案

Hide That Number

热度:0   发布时间:2024-01-13 21:11:24.0

1/http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2090

2、简单题,错在字符数组a[0]'-0'+3;这样就可以实现字符转换成数字

因为多了一个for循环将字符数组转换成整数,超时了

3、题目:

OnlineJudge调查问卷

Hide That Number

Time Limit: 1000MS    Memory limit: 65536K

题目描述

According to Wikipedia, Cryptography is “the practice and study of hiding information” and this is exactly what Alex is looking for. Ever since he was a kid, Alex was paranoid over someone laying their hands on his phone book. He decided that he must write the numbers in some secret way that only he can decipher. At first he tried quite complex algorithms, but that slowed him down when he needed to dial a number fast. He finally came up with the following algorithm: Rather than writing down the number itself, Alex would shift the number one place to the left (as if multiplying it by 10,) then adding the shifted number to the original. For example, if the phone number was 123, Alex would add 1230 to it, resulting in 1353. To make what he writes looks as a regular phone number, Alex truncates the result (from the left,) so that it has as many digits as the original phone number. In this example, Alex writes 353 instead of 123 in his phone book.
Alex needs a program to print the original phone number given what is written in his phone book.  Alex, who by the way is a good friend of Johnny, isn’t that good in arithmetic. It is quite possible that the numbers are messed up. The program should print "IMPOSSIBLE" (without the quotes) if the original number cannot be computed.

输入

Your program will be tested on one or more test cases. Each case is specified on a separate line and is made of a single positive number having less than 1, 000, 000 digits. The last line of the input file is made of a single zero.

输出

For each test case, output the result on a single line using the following format:
k._result
Where k is the test case number (starting at 1) and _ is a single space.

示例输入

353
9988
123456
0

示例输出

1. 123
2. IMPOSSIBLE
3. 738496

 

4、错的代码:

01.#include<stdio.h>  
02.#include<string.h>  
03.char str[1000010];  
04.int a[1000010];  
05.int b[1000010];  
06.int main()  
07.{  
08.    int cas=0;  
09.    while(scanf("%s",str))  
10.    {  
11.        cas++;  
12.        if(str[0]=='0')  
13.            break;  
14.        for(int i=0; i<strlen(str); i++)  
15.        {  
16.            b[i]=str[i]-'0';  
17.        }  
18.        int len=strlen(str)-1;  
19.        a[len]=b[len];  
20.        for(int j=len-1; j>=0; j--)  
21.        {  
22.            if(b[j]-a[j+1]>=0)  
23.            {  
24.                a[j]=b[j]-a[j+1];  
25.            }  
26.            else  
27.            {  
28.                a[j]=b[j]+10-a[j+1];  
29.                if(j-1>=0)  
30.                b[j-1]--;  
31.            }  
32.        }  
33.        printf("%d. ",cas);  
34.        if(a[0]==0)  
35.            printf("IMPOSSIBLE\n");  
36.        else  
37.        {  
38.            for(int i=0; i<=len; i++)  
39.                printf("%d",a[i]);  
40.            printf("\n");  
41.        }  
42.    }  
43.    return 0;  
44.}  
45.  


对的代码:

#include<stdio.h>
#include<string.h>
char str[1000010];
int a[1000010];
char b[1000010];
int main()
{int cas=0;while(scanf("%s",b)){cas++;if(b[0]=='0')break;int len=strlen(b)-1;a[len]=b[len]-'0';for(int j=len-1; j>=0; j--){if(b[j]-'0'-a[j+1]>=0){a[j]=b[j]-'0'-a[j+1];}else{a[j]=b[j]-'0'+10-a[j+1];if(j-1>=0){b[j-1]--;}}}printf("%d. ",cas);if(a[0]==0)printf("IMPOSSIBLE\n");else{for(int i=0; i<=len; i++)printf("%d",a[i]);printf("\n");}}return 0;
}
/*
353
9988
123456
0
*/


 

  相关解决方案