啊啊······只有一个AC,请大佬指点吧【求求了】【我太难了】

P1055 [NOIP2008 普及组] ISBN 号码

MyConceptCOMEON @ 2024-02-16 15:30:50

HELP!!!

#include<bits/stdc++.h>
using namespace std;
char isbn[20];
char sbm[20]={'0','1','2','3','4','5','6','7','8','9','X'};
int main(){
    int cc=1,sum=0;
    for(int i=1;i<=13;i++){
        cin>>isbn[i];
    }
    for(int i=1;i<=13;i++){
        if(isbn[i]=='-') continue;
        else{
            sum=((sum+isbn[i])-'0')*cc;
            cc++;
        }
    }
    sum%=11;
    if(sbm[sum]==isbn[13]){
        cout<<"Right"<<endl;
        return 0;
    }
    else{
        for(int i=1;i<=13;i++){
            if(i==13){
                cout<<sbm[sum];
                return 0;
            } 
            cout<<isbn[i];
        }
    }
    return 0;
} 

by Apress @ 2024-02-16 15:41:44

@MyConceptCOMEON

    for(int i=1;i<=13;i++){
        if(isbn[i]=='-') continue;
        else{
            sum=((sum+isbn[i])-'0')*cc;
            cc++;
        }
    }

破案了,循环那里改到12就AC了


by MyConceptCOMEON @ 2024-02-16 15:56:28

@Apress 【无能为力】【救救我】

#include<bits/stdc++.h>
using namespace std;
char isbn[20];
char sbm[20]={'0','1','2','3','4','5','6','7','8','9','X'};
int main(){
    int cc=1,sum=0;
    char chaR;
    for(int i=1;i<=13;i++){
        cin>>isbn[i];
    }
    for(int i=1;i<=13;i++){
        if(isbn[i]=='-') continue;
        else{
            chaR=isbn[i]-'0';
            sum=sum+chaR*cc;
            cc++;
        }
    }
    sum%=11;
    if(sbm[sum]==isbn[13]){
        cout<<"Right"<<endl;
        return 0;
    }
    else{
        for(int i=1;i<=13;i++){
            if(i==13){
                cout<<sbm[sum];
                return 0;
            } 
            cout<<isbn[i];
        }
    }
    return 0;
} 

by Apress @ 2024-02-16 15:59:05

@MyConceptCOMEON

#include<bits/stdc++.h>
using namespace std;
char isbn[20];
char sbm[20]={'0','1','2','3','4','5','6','7','8','9','X'};
int main(){
    int cc=1,sum=0;
    for(int i=1;i<=13;i++){
        cin>>isbn[i];
    }
    for(int i=1;i<=12;i++){
        if(isbn[i]=='-') continue;
        else{
            sum+=(isbn[i]-'0')*cc;
            cc++;
        }
    }
    sum%=11;
    if(sbm[sum]==isbn[13]){
        cout<<"Right"<<endl;
        return 0;
    }
    else{
        for(int i=1;i<=13;i++){
            if(i==13){
                cout<<sbm[sum];
                return 0;
            } 
            cout<<isbn[i];
        }
    }
    return 0;
} 

by Apress @ 2024-02-16 15:59:52

@MyConceptCOMEON

你WA是因为把最后一位也算进sum里面了


by MyConceptCOMEON @ 2024-02-16 16:05:35

@Apress 【全体起立!为Apress鞠躬】 感谢感谢!!!

来我团队好不好,求求了,私信


by Kete @ 2024-02-18 12:11:23

@MyConceptCOMEON

不是...你在B2115 密码翻译提出过问题我给你回答过了...

我是这样写的:

#include<bits/stdc++.h>
using namespace std;
int main(){
    char a[14],mod[12]="0123456789X";
    scanf("%s",a);
    int i,j=1,t=0;
    for(i=0;i<12;i++){
        if(a[i]=='-') continue;
        t+=(a[i]-48)*j++;
    }
    if(mod[t%11]==a[12]) cout<<"Right";
    else
    {
        a[12]=mod[t%11];
        printf("%s",a);
    }
    return 0;
}

以上是全AC代码


|