再次求助

P1055 [NOIP2008 普及组] ISBN 号码

_Glassy_Sky_ @ 2023-08-14 21:12:13

#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    string s;
    int cnt = 1, ans = 0;
    cin >> s;
    for(int i = 0; i < s.size() - 2; i ++)
    {
        if(s[i] != '-')
        {
            int x = int(s[i]) - 48;
            ans += cnt * x;
            cnt ++;
        }
    }
    int sum = ans % 11;
    if(s[s.size() - 1] == 'X')
        if(sum == 10)
        {
            cout << "Right";
            return 0;
        }
        else
        {
            for(int i = 0; i < s.size() - 1; i ++)
                cout << s[i];
            cout << sum;
            return 0;
        }
    else
        if(int(s[s.size() - 1] - 48) == sum)
        {
            cout << "Right";
            return 0;
        }
        else
        {
            for(int i = 0; i < s.size() - 1; i ++)
                cout << s[i];
            cout << sum;
            return 0;
        }
    return 0;
}

by aikyon_lz2022 @ 2023-08-29 20:48:45

@FZwangmuem @FZwangmuem 你需要在倒数6-7行中间特别判断sum==10的情况将其输出改为x 我这里写了一下


else { for(int i = 0; i < s.size() - 1; i ++) cout << s[i]; if(sum==10) cout<<"X"; else cout << sum; return 0; }

by aikyon_lz2022 @ 2023-08-29 20:50:10

@FZwangmuem 有点乱见谅


|