c++

P1055 [NOIP2008 普及组] ISBN 号码

Mark_666 @ 2023-05-10 17:54:15

#include<bits/stdc++.h>
using namespace std;
string s;
int t=1;
int sum;
int main()
{
    cin>>s;
    for(int i=0;i<s.size()-1;i++)
    {
        if(s[i]-'0'>=0&&s[i]-'0'<=9)
        {
            sum+=(s[i]-'0')*t;
            t++;
        }
    }
    sum=sum%11;
    if(sum==10)
    {
        if(s[s.size()-1]=='X')
            cout<<"Right";
        else
        {
            for(int i=0;i<s.size()-1;i++)
                cout<<s[i]-'0';
            cout<<'X';
        }
    }
    else
    {
        if(s[s.size()-1]-'0'==sum)
            cout<<"Right";
        else
        {
            for(int i=0;i<s.size()-1;i++)
                cout<<s[i]-'0';
            cout<<sum;
        }
    }
    return 0;
}

by jcls @ 2023-05-10 18:27:27

@Mark_666 输出时不用打s[i]-'0',改为s[i]就能过了


by Resolute_Faith @ 2023-05-10 18:33:04

。?


by rnf5114 @ 2023-05-10 18:36:34

@Mark_666 又是我,照着你的代码稍微改了一些,即可AC

#include<bits/stdc++.h>
using namespace std;
string s;
int t=1;
long long sum;
int main(){
    cin>>s;
    for(int i=0;i<12;i++){
        if(s[i]-'0'>=0&&s[i]-'0'<=9){
            sum+=(s[i]-'0')*t;
            t++;
        }
    }
    sum=sum%11;
    if(sum==10){
        if(s[12]=='X')
            cout<<"Right";
        else{
            for(int i=0;i<12;i++)
                cout<<s[i];
            cout<<'X';
        }
    }
    else{
        if(s[12]-'0'==sum)
            cout<<"Right";
        else{
            for(int i=0;i<12;i++)
                cout<<s[i];
            cout<<sum;
        }
    }
    return 0;
}

by Mark_666 @ 2023-05-11 21:41:34

@liyong5114 为啥输出不能-'0'啊,没搞懂


by rnf5114 @ 2023-05-13 09:52:15

@Mark_666下划线可能会被减


by rnf5114 @ 2023-05-13 09:52:23

@Mark_666


by xiaoshumiao @ 2023-05-25 16:29:53

@Mark_666 因为要输出'-'的话,就不能用"-'0'"了


|