题解 | #[NOIP2008]ISBN号码#

[NOIP2008]ISBN号码

https://www.nowcoder.com/practice/95712f695f27434b9703394c98b78ee5

#include <cstdio>
#include <iostream>
#include <ostream>
#include <string>
using namespace std;

int main() {
    int a, b, c;
    char d;
    while (scanf("%d-%d-%d-%c",&a,&b,&c,&d)==4)
    { // 注意 while 处理多个 case
        string e;
        e = to_string(a)+to_string(b)+to_string(c);
        int f=0;
        for(int i = 0;i<9;i++)
        {
            f += (e[i]-'0')*(i+1);
        }
        if(f%11==10 && d=='X')
        {
            cout<<"Right"<<endl;
        }
        else if(f%11==(d-'0'))
        {
            cout<<"Right"<<endl;
        }
        else if(f%11==10 && d!='X')
        {
            char g='X';
            printf("%d-%d-%d-%c",a,b,c,g);
        }
        else {
            printf("%d-%d-%d-%d",a,b,c,f%11);
        }
    }
}
// 64 位输出请用 printf("%lld")

注意:如果使用string e=“1234”,e[0]=1应该是e[0]='1',想得到e[0]=1,应该是e[0]-'0'。另外可以多看看逻辑

全部评论

相关推荐

这就是伊泽瑞尔:进什么厂,就是三方面决定,八股+项目可以进中小厂,要进大厂再刷刷算法。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务