B3850 [GESP202306 四级] 幸运数 题解

· · 题解

前言

这是本蒟蒻第一篇题解。希望大家支持我。\ 看来没有人用字符串读入的,我就用了字符串。

思路

这道题目,主打一个纯模拟。\ 但是有几个易错点,我会写在注释里,请各位阅读,谢谢。

代码(+注释)

#include <iostream>
#include <string>
using namespace std;
char a[19]={'0','7','5','3','1','8','6','4','2','9'};
//我通过手算完成了每一位的变换结果。
int main(){
    int T;
    cin >> T;//注意:多组测试数据。
    for (int t=1; t<=T; t++){
        string s;
        cin >> s;
        int top=s.size()-1;//注意:字符串从0开始计数,所以要-1!
        for (int i=top; i>=0; i-=2){//从尾巴来,隔一位看一位。
            s[i]=a[s[i]-'0'];//要减去一个字符0,我在这里卡了一刻钟。
        }
        int sum=0;
        for (int i=0; i<=top; i++){
            sum+=s[i];
            sum-='0';//同理,这里要减去字符0
        }
        if (sum%8==0)   cout << "T\n";
        else    cout << "F\n";
    }

}