Codeforces 625 D Finals in arithmetic
2016-02-08 12:43
429 查看
传送门:
http://codeforces.com/contest/625/problem/D
题意:
一个数加上倒过来的数得到一个数!现让你还原这个数!
这是一道思维题,建设性算法的题
很简单,恢复原序列的方法就是根据对称的位看合法否,然后将和除以二就还原成功了,具体处理的时候需要注意有进位,因此讨论两种进位的情况,逐个还原成对应位的实际和 就ok了!最后还原的时候还要单独处理奇数位最中间的那一位,然后其余对应两两处理就ok了!
http://codeforces.com/contest/625/problem/D
题意:
一个数加上倒过来的数得到一个数!现让你还原这个数!
这是一道思维题,建设性算法的题
很简单,恢复原序列的方法就是根据对称的位看合法否,然后将和除以二就还原成功了,具体处理的时候需要注意有进位,因此讨论两种进位的情况,逐个还原成对应位的实际和 就ok了!最后还原的时候还要单独处理奇数位最中间的那一位,然后其余对应两两处理就ok了!
#include <cstdio> #include <cstring> char s[100010]; char ans[100010]; int sum[100010]; int n; bool check() { for (int i = 0; i < n / 2;) { if (sum[i] == sum[n - 1 - i])// 789 987 78 87 ++i; else if (sum[i] == sum[n - 1 - i] + 1 || sum[i] == sum[n - 1 - i] + 10 + 1) { sum[i]--; sum[i + 1] += 10; } else if (sum[i] == sum[n - 1 - i] + 10) { sum[n - 2 - i]--; sum[n - 1 - i] += 10; } else return false; } if (n % 2 == 1) { if (sum[n / 2] % 2 == 1 || sum[n / 2] > 18 || sum[n / 2] < 0) return false; else ans[n / 2] = sum[n / 2] / 2 + '0'; } for (int i = 0; i < n / 2; ++i) { if (sum[i] > 18 || sum[i] < 0) return false; ans[i] = (sum[i] + 1) / 2 + '0'; ans[n - 1 - i] = sum[i] / 2 + '0'; } return ans[0] > '0'; } int main() { scanf("%s", s); n = strlen(s); for (int i = 0; i < n; ++i) sum[i] = s[i] - '0'; if (check()) puts(ans); else if (s[0] == '1' && n > 1) { for (int i = 0; i < n; ++i) sum[i] = s[i + 1] - '0'; n--; sum[0] += 10; if (check()) puts(ans); else puts("0"); } else puts("0"); return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima