2013春季SD高校ACM周赛9(SDUT) -b

来源:互联网 发布:贝佳斯原理 知乎 编辑:程序博客网 时间:2024/04/30 06:31

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.

示例输入

35399881234560

示例输出

1. 1232. IMPOSSIBLE3. 738496

开始的时候把1, 000, 000 digits理解为小于1, 000, 000 的整数 结果悲剧了~ 用字符串处理的方法移位借位

code:

 

#include<iostream>#include<vector>#include<queue>#include<stack>#include<stdio.h>#include<algorithm>#include<cstdio>#include<string>#include<cstring>using namespace std;//头文件int main(){char str[1000009];char str2[1000009];int len;int j;int T=0;while(cin>>str)//开始的时候使用的是gets()接受字符 结果wa了十几遍 gets()不行么? {j=0;if(strcmp(str,"0")==0) break;len=strlen(str);str2[0]=str[len-1];for(int i=len-2;i>=0;i--){if(str[i]>=str2[j]){str2[j+1]=((str[i]-'0')-(str2[j]-'0'))+'0';j++;}else{str2[j+1]=((str[i]-'0')+10-(str2[j]-'0'))+'0';str[i-1]--;//尝试了一个字符串直接-- 这样也可以  //str[i-1]=((str[i-1]-'0')-1)+'0';j++;}}cout<<++T<<". ";if(str2[len-1]!='0')for(int i=len-1;i>=0;i--)cout<<str2[i];elsecout<<"IMPOSSIBLE";cout<<endl;}return 0;} 


 

 

 

 

 

 

 

 

 

原创粉丝点击