帮我看看我代码哪里有问题吧,只有七十分,感谢感谢感谢

P1055 [NOIP2008 普及组] ISBN 号码

QYlucky @ 2022-04-04 17:40:56

#include<iostream>
using namespace std;
char a[20];
char b[20];
int main() {
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++){
        a[i]=s[i];
    }
    int m=0;
    for(int i=0;i<s.length();i++){
        if(a[i]!='-'){
            b[m++]=a[i];
        }
        else{
            b[m++]=a[++i];
        } 
    }
    int sum=0;
    for(int i=0;i<m-1;i++){
        sum=sum+(b[i]-'0')*(i+1);
    }
    if((b[m-1]-'0')==(sum%11)||((sum%11)==10&&b[m-1]=='X')){
        cout<<"right"<<endl;
    }
    else{
        if(sum%11==10){
        a[s.length()-1]='X';    
        }
        else{
        a[s.length()-1]=(sum%11)+'0';
        }

    for(int i=0;i<s.length();i++){
        cout<<a[i];
      }
    }

    return 0;
}

by AKNOI的梓钦 @ 2022-04-04 17:48:54

#include<iostream>
using namespace std;
char a[20];
char b[20];
int main() {
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++){
        a[i]=s[i];
    }
    int m=0;
    for(int i=0;i<s.length();i++){
        if(a[i]!='-'){
            b[m++]=a[i];
        }
        else{
            b[m++]=a[++i];
        } 
    }
    int sum=0;
    for(int i=0;i<m-1;i++){
        sum=sum+(b[i]-'0')*(i+1);
    }
    if((b[m-1]-'0')==(sum%11)||((sum%11)==10&&b[m-1]=='X')){
        cout<<"right"<<endl;//Right
    }
    else{
        if(sum%11==10){
        a[s.length()-1]='X';    
        }
        else{
        a[s.length()-1]=(sum%11)+'0';
        }

    for(int i=0;i<s.length();i++){
        cout<<a[i];
      }
    }

    return 0;
}

by Avocadooo @ 2022-04-04 17:51:50

输出 Right 而非 right @QYlucky


by QYlucky @ 2022-04-04 18:35:12

@乔木生夏凉 啊原来如此 感谢感谢感谢!!!


by jingchuan_Hou @ 2022-04-06 14:33:36

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string isbn;
    cin >> isbn;

    //依此将数字提取到数组中 
    int num[9];
    num[0] = isbn[0] - '0';

    for (int i = 2; i < 5; i++)
    {
        //cout << isbn[i];
        num[i - 1] = isbn[i] - '0';
    }
    //cout << endl;
    for (int i = 6; i < 11; i++)
    {
        //cout << isbn[i];
        num[i - 2] = isbn[i] - '0';
    }
    //cout << endl;
    /*for (int i = 0; i < 9; i++)
    {
        cout << num[i];
    }*/

    //算出余数 
    int sum = 0;
    for (int i = 0; i < 9; i++)
    {
        sum += num[i] * (i + 1);
    }

    int flag = sum % 11;

    //cout << flag << endl;

    //判断余数为10的情况 
    if (flag == 10)
    {
        if ('X' == isbn[12]) cout << "Right";
        else
        {
            isbn[12] = 'X';
            cout << isbn;
        }
    }
    else
    {
        if (flag == isbn[12] - '0') cout << "Right";
        else
        {
            isbn[12] = flag + '0';
            cout << isbn;
        }
    }

    return 0;
}

|