2019-08-27 A1005 Spell It Right

2019-08-27  本文已影响0人  JO炮

也是一道相对简单的题目,这道题容易出错的点是字符串与整型的转换:

    for(int i = 1; i < re.length(); i++){
        cout << " " << op[re[i] - '0'];
    }

不要忘记op[re[i]] - '0'

using namespace std;
int main() {
    string s, re;
    string op[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    cin >> s;
    int t = 0;
    for(int i = 0; i < s.length(); i++){
        t += (s[i] - '0');
    }
    re = to_string(t);
    cout << op[re[0] - '0'];
    for(int i = 1; i < re.length(); i++){
        cout << " " << op[re[i] - '0'];
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读