贪心/字符串处理 Codeforces Round #291 (Div. 2) A. Chewbaсca and Number

本文探讨了在处理字符串时遇到的常见问题,并通过优化算法解决了字符串转换中出现的错误,包括WA(Wrong Answer)情况。文章详细介绍了从输入读取到输出处理的完整流程,特别关注了字符串处理中的细节,如使用`''`的正确性以及如何避免常见的编程陷阱。

 

题目传送门

 1 /*
 2     WA了好几次,除了第一次不知道string不用'\0'外
 3     都是欠考虑造成的
 4 */
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <iostream>
10 #include <algorithm>
11 #include <map>
12 #include <set>
13 #include <vector>
14 using namespace std;
15 
16 int main(void)
17 {
18     //freopen ("A.in", "r", stdin);
19     
20     string s, ans;
21     
22     while (getline (cin, s))
23     {
24         ans = "";
25         for (int i=0; s[i]!='\0'; ++i)
26         {
27             int x = s[i] - '0';
28             if (x > (9 - x))
29             {
30                 if (i == 0 && x == 9)
31                     ans += s[i];
32                 else
33                     ans += ((9 - x) + '0');
34             }
35             else
36                 ans += s[i];
37         }
38         //ans += '\0';
39         cout << ans << endl;
40     }
41     
42     return 0;
43 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4366716.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值