2016 Winter Training Day #1_E题_codefcrces 514A(贪心)
2016-01-21 19:40
465 查看
A. Chewbaсca and Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means
replacing it with digit 9 - t.
Help Chewbacca to transform the initial number x to the minimum possible positive number
by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.
Input
The first line contains a single integer x (1 ≤ x ≤ 1018) —
the number that Luke Skywalker gave to Chewbacca.
Output
Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.
Sample test(s)
input
output
input
output
4444
题意:给出一个数字,对这个数字的每一位都可以有两个操作:保持原数字,或者翻转(用9减该位数字)。求最后求出来的数字的最小值。
思路:简单贪心,只需要每一位数字都最小,则整个数字肯定最小。对每一位数字都取 min(a, 9-a)就ok了,非常简单,但我用字符串做的时候,总是输出不了样例。
for(int i = 0; i < s.size(); ++i)
s[i] = min(s[i], '9'-s[i]);
cout << s << endl;
错误原因:对ASCII码不够熟悉,'9'-s[i] 得出来的是 数字 1 2 3 4 5 .... 而不是字符 ‘1’‘2’,所以还应该改为
s[i] = min(s[i], '9'-s[i]+'0')
代码:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means
replacing it with digit 9 - t.
Help Chewbacca to transform the initial number x to the minimum possible positive number
by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.
Input
The first line contains a single integer x (1 ≤ x ≤ 1018) —
the number that Luke Skywalker gave to Chewbacca.
Output
Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.
Sample test(s)
input
27
output
22
input
4545
output
4444
题意:给出一个数字,对这个数字的每一位都可以有两个操作:保持原数字,或者翻转(用9减该位数字)。求最后求出来的数字的最小值。
思路:简单贪心,只需要每一位数字都最小,则整个数字肯定最小。对每一位数字都取 min(a, 9-a)就ok了,非常简单,但我用字符串做的时候,总是输出不了样例。
错误代码:
for(int i = 0; i < s.size(); ++i)
s[i] = min(s[i], '9'-s[i]);
cout << s << endl;
错误原因:对ASCII码不够熟悉,'9'-s[i] 得出来的是 数字 1 2 3 4 5 .... 而不是字符 ‘1’‘2’,所以还应该改为
s[i] = min(s[i], '9'-s[i]+'0')
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <cmath> #include <map> #include <set> using namespace std; const int INF = 0x7fffffff; int main() { char s[20]; cin >> s; int n = strlen(s); for(int i = 0; i < n; ++i) { if(i == 0 && s[i] == '9') continue; else { if(s[i] >= '5') s[i] = '9'- s[i] +'0'; else continue; } } cout << s << endl; return 0; }
相关文章推荐
- 2016 Winter Training Day #1_D题_codefcrces 432A(贪心)
- org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
- Cannot add or update a child row: a foreign key constraint fails
- 2016 Winter Training Day #1_B题_codefcrces 588A(贪心)
- 2016 Winter Training Day #1_A题_codefcrces 230A(贪心)
- CodeForces 164A Variable, or There and Back Again 搜索
- 最新Google Gmail邮箱登陆不了解决方法
- RETAIN语句
- 美国末日AI System设计分享
- width、clientWidth、offsetWidth、scrollWidth、availWidth
- Examine the following statement that is used to modify the primary key constraint on the SALES tabl
- Could not launch process failed:security
- Error:Execution failed for task ':transformClassesWithJarMergingForDebug'
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-c
- Error:Execution failed for task ':transformClassesWithDexForDebug'.
- 超级无敌qmail安装大法,详尽之极
- [精华] qmail安装心得(安装过程)
- sendmail发送邮件
- sendmail-attachment.py――发送带附件的邮件
- OpenCV Error: Assertion failed