poj 3508 Hide That Number(模拟大数减法)
2017-02-15 14:42
555 查看
题目链接
http://poj.org/problem?id=3508题意:
给出一个数字,这个数字的前几位被擦去了,求能否得到一个x,让x*10+x等于这个数字原来的样子题解:
被擦去的几位只可能是1-10这10个数字,用99,999之类的数举个例子就可以了,然后仔细观察这题发现有个规律:a b c 0
a b c
———- (a>0)
b c c
a d
因此,能求到x的情况下,最后的x首位必然不能为0,其他位如果减去后一位数字后小于0向前一位借1即可
注意点
输出的char数组要在最后一位输入一个“\0”AC代码
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; char num[1000010]; int main() { int cnt = 1; while(scanf("%s", num)) { //cout << num <<endl; int len = strlen(num); char ans[1000010]; //cout << len << endl; if(strcmp(num, "0") == 0)break; ans[len-1] = num[len-1]; int sigh = 0; int number = num[len-1]; for(int i = len - 2; i >= 0; i--) { number = num[i] - number + sigh; if(number < 0) { ans[i] = number + 10 + '0'; sigh = -1; } else { ans[i] = number + '0' ; sigh = 0; } number = ans[i]; } ans[len] = '\0'; if (ans[0] != '0') printf("%d. %s\n",cnt++,ans); else printf("%d. IMPOSSIBLE\n",cnt++); } return 0; }
相关文章推荐
- POJ 3508 Hide That Number 可能会
- POJ 3508 Hide That Number
- Name That Number(模拟)(USACO)
- 【USACO-Chapter1-1.2】【模拟】Name That Number
- POJ 2506 Tiling (递推 + 大数加法模拟 )
- hdu 4920 Ugly Problem [模拟+大数减法]
- POJ 1350 Cabric Number Problem(模拟)
- poj 1019 Number Sequence && hdu 1597 find the nth digit(二分,模拟)
- poj 2413 How many Fibs? 大数累加模板+字符串模拟大数比较大小
- poj 1001 -- Exponentiation (大数计算,模拟)
- poj Number Sequence 1019 (打表 &模拟)
- POJ 2506 Tiling(大数递推&&(数组模拟||JAVA))
- POJ 1049 Round and Round We Go 大数模拟
- POJ 1016 Numbers That Count 模拟题目
- Hide That Number
- POJ 2756 Autumn is a Genius 采用string大数减法
- POJ 1016 Numbers That Count 模拟
- HDU 5920 Ugly Problem(大数减法 模拟)
- Hide That Number
- poj 2413 大数模拟(区间内的斐波那契数个数)