poj3580

Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
1207020203      Log Out
Mail:0(0)
Login Log      Archive
Language:
Hide That Number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4943 Accepted: 1510

Description

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.

Input

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.

Output

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).

Sample Input

353
9988
123456
0

Sample Output

1. 123
2. IMPOSSIBLE
3. 738496

#include<stdio.h>
#include<string.h>
#include<malloc.h>
char a[1000010],*b;
main()
{
	long i,j,k,z,count=1,flag=0;
	//freopen("D:\\p.txt","r",stdin);
	while(scanf("%s",&a)&&strcmp(a,"0")!=0)
	{
		b=(char *)malloc(strlen(a)+1);
		b[strlen(a)]='\0';
		j=strlen(a)-1;k=0;memset(b,0,sizeof(b));
		b[j]=a[strlen(a)-1];
		for(i=strlen(a)-2;i>=0;i--,j--)
		{
			if(k) {a[i]=a[i]-1;k=0;}
			if(a[i]-b[j]<0) {a[i]=a[i]+10;flag=1;}
			b[j-1]=a[i]-b[j]+'0';
			if(flag)  {k=1;flag=0;}
		}
		if(b[j]!='0')
		{
			printf("%d. %s\n",count,b);
		}
		else printf("%d. IMPOSSIBLE\n",count);
		count++;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值