Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心
2016-02-08 16:31
746 查看
D. Finals in arithmetic
题目连接:
http://www.codeforces.com/contest/625/problem/DDescription
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.
As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).Output
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.Sample Input
4Sample Output
2Hint
题意
给你一个数k,要求你找到一个数x,使得x+反着的x = k比如33,你就可以找21,因为21+12=33
不允许前导0
题解:
贪心,我们先不管前导0这个条件,我们如果sum[i]==sum[n-i-1]的话,ans[i]=(sum[i]+1)/2,ans[n-i-1]=sum[i]/2就好了如果不相等的话,我们应该怎么呢?我们需要考虑进位
要么从后一位进1,要么从前一位退10回来,就这两种,讨论一下就好了
注意165这种数据,1开头的
代码
#include<bits/stdc++.h> using namespace std; const int maxn = 1e6+7; char s[maxn]; char ans[maxn]; int sum[maxn]; int n; int check() { for(int i=0;i<n/2;) { int l=i,r=n-1-i; if(sum[l]==sum[r]) i++; else if(sum[l]==sum[r]+1||sum[l]==sum[r]+11)//考虑从后面进了一位,11 = 1+10 { sum[l]--; sum[l+1]+=10; } else if(sum[l]==sum[r]+10)//考虑从R前面退一位 { sum[r-1]--; sum[r]+=10; } else return 0; } if(n%2==1) { if(sum[n/2]%2==1||sum[n/2]>18||sum[n/2]<0)return 0; ans[n/2]=sum[n/2]/2+'0'; } for(int i=0;i<n/2;i++) { if(sum[i]>18||sum[i]<0)return 0; ans[i]=(sum[i]+1)/2+'0'; ans[n-i-1]=(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())return puts(ans); if(s[0]=='1'&&n>1)//首位为1的时候 { for(int i=0;i<n;i++) sum[i]=s[i+1]-'0'; sum[0]+=10; n--; if(check())puts(ans); else printf("0"); } else printf("0"); }
相关文章推荐
- ES6学习——总结
- 【慕课笔记】U3 继承 第4节 JAVA中的final的使用
- lighttpd-1.4.39 : watcher and worker
- Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04
- NYOJ 76 超级台阶
- 与教宗的对话之找寻天主的路径
- Tomcat7源码分析(二)组件生命周期详解
- JAVA设计模式实战---工厂模式!!!
- [BZOJ2434][NOI2011]阿狸的打字机
- java项目经验总结之自定义异常类
- C#成神之路<15> C#异常处理调试
- [LeetCode]39. Combination Sum
- 矩阵乘法经典应用之坐标变化
- volley中多级别取消请求Request
- 中科院ICTCLA授权文件更新网址
- 注解和反射
- IT痴汉的工作现状44-外包困局
- 标准C++中的string类的用法总结
- nyoj 1112
- 1013. 数素数