Hide That Number

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
*/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值