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;}
- 2013春季SD高校ACM周赛9(SDUT) -b
- 2013春季SD高校ACM周赛9(SDUT) -A
- 2013春季SD高校ACM周赛9(SDUT) -E
- 【2013春季SD高校ACM周赛8(SDUT)】各水题题解
- 2013春季SD高校ACM周赛8(SDUT)
- 2013寒假SD高校ACM周赛3(SDUT)
- 【2013寒假SD高校ACM周赛4(SDJZU & SDUT)】 超级玛丽
- 2013寒假SD高校ACM周赛3—Steps
- 【读题坑爹】 2013寒假SD高校ACM周赛总结
- 【打表找规律】2013寒假SD高校ACM周赛5——I’ve Got Your Back(gammon)
- 【再看细节】SDUT春季高校周赛9 Problem E——A Tale from the Dark Side of The Moon
- 2013 CSDN & 西南大学 高校俱乐部 春季巡讲
- 2013 山东科技大学CSDN高校俱乐部春季巡讲
- 【差一点就坑了】SDUT春季高校周赛8(SDUT2038) Problem F——数字游戏
- 山东大学(威海)CSDN高校俱乐部2013年春季学期第一次会员大会
- 2013年天津工业大学高校俱乐部春季学期巡讲实录
- 高校ACM题库
- 高校ACM题库
- C#操作XML小结
- 长方柱类
- USB 驱动学习开始(一)
- 读《写给24的自己》有感
- 一个Demo让你掌握所有的android控件
- 2013春季SD高校ACM周赛9(SDUT) -b
- 黑马程序员_正则表达式(regex)
- shell脚本调试方法
- log4j配置示例
- FTP 工具类
- PHPMailer - PHP email transport class
- 谷歌三大核心技术(一)Google File System中文版
- 2013春季SD高校ACM周赛9(SDUT) -E
- ubuntu10.04下安装windows7