题解

281 条题解

  • 0
    @ 2019-08-02 10:22:23
    isbn = input()
    i = 1
    y = 0
    for s in isbn:
        if i == 10:
            break
        if s != '-':
            y += int(s) * i
            i += 1
    m = y % 11
    
    if m == 10:
        if isbn[-1] == 'X':
            print('Right')
        else:
            isbn = isbn[0:-1]+'X'
            print(isbn)
    else:
        if str(m) == isbn[-1]:
            print('Right')
        else:
            isbn = isbn[0:-1]+str(m)
            print(isbn)
    
  • 0
    @ 2019-06-12 19:04:00
    //judge the identification code
    #include <iostream>
    #include <vector>
    int main(){
        int sum = 0, sign;
        std::string s;
        std::vector<char> v;
        getline(std::cin, s);
        for(int i = 0; i < s.size(); i++)
            if(s[i] != '-') v.push_back(s[i]);
        for(int i = 1; i <= 9; i++)
            sum += (v[i - 1] - '0') * i;
        sign = sum % 11;
        if(sign == 10 && v[9] == 'X' || sign == v[9] - '0')
            std::cout << "Right" << std::endl;
        else{
            s[s.size() - 1] = (sign == 10 ? 'X' : (sign + 48));
            std::cout << s << std::endl; 
        }
        return 0;
    }
    
  • 0
    @ 2019-01-21 09:53:45

    #include <stdio.h>

    int main()
    {
    char s[20];
    scanf("%s",s);
    int rst=0;
    int a[20];
    int i=0,j=0;
    while(s[j]!=0)
    {
    if(s[j]=='-')
    j++;
    else
    {
    a[i]=s[j]-'0';
    i++,j++;
    }
    }
    int flag1=0,flag2=0;//必须初始化,差点坑在上面
    if(s[j-1]=='X')
    flag1=1;
    for(i=0; i<9; i++)
    rst+=a[i]*(i+1);
    rst=rst%11;
    if(rst==10)
    flag2=1;
    if(a[9]==rst || (flag1==1 && flag2==1))
    printf("Right\n");
    else
    {
    for(i=0; i<12; i++)
    putchar(s[i]);
    if(rst<10)
    printf("%d\n",rst);
    else
    printf("X\n");
    }

    return 0;
    }

  • 0
    @ 2019-01-19 09:44:07

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

    int main()
    {
    char reg[11]={'0','1','2','3','4','5','6','7','8','9','X'};
    //定义一个数组储存结果0-X
    string ss;
    cin>>ss;
    string::iterator it;//定义指向string类的迭代器,可以理解为指针
    for(it=ss.begin();it!=ss.end();it++){
    if(it=='-'){ss.erase(it);}
    //erase(it)可以理解为删除指向it位置的字符
    }
    int sum=0;
    for(int i=0;i<9;i++){
    sum=sum+(ss[i]-'0')
    (i+1);//字符数字-'0'代表转换成数学数字
    }
    sum=sum%11;
    if(reg[sum]==ss[9]){//如果和最后一位相等,则输出正确
    cout<<"Right"<<endl;
    }
    else{
    cout<<ss[0]<<"-"<<ss[1]<<ss[2]<<ss[3]<<"-"<<ss[4]<<ss[5]<<ss[6]<<ss[7]<<ss[8]<<"-"<<reg[sum]<<endl;
    }
    return 0;
    }

  • 0
    @ 2019-01-19 09:42:57

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

    int main()
    {
    char reg[11]={'0','1','2','3','4','5','6','7','8','9','X'};
    //定义一个数组储存结果0-X
    string ss;
    cin>>ss;
    string::iterator it;//定义指向string类的迭代器,可以理解为指针
    for(it=ss.begin();it!=ss.end();it++){
    if(it=='-'){ss.erase(it);}
    //erase(it)可以理解为删除指向it位置的字符
    }
    int sum=0;
    for(int i=0;i<9;i++){
    sum=sum+(ss[i]-'0')
    (i+1);//字符数字-'0'代表转换成数学数字
    }
    sum=sum%11;
    if(reg[sum]==ss[9]){//如果和最后一位相等,则输出正确
    cout<<"Right"<<endl;
    }
    else{
    cout<<ss[0]<<"-"<<ss[1]<<ss[2]<<ss[3]<<"-"<<ss[4]<<ss[5]<<ss[6]<<ss[7]<<ss[8]<<"-"<<reg[sum]<<endl;
    }
    return 0;
    }

  • 0
    @ 2019-01-15 13:23:55

    #include<iostream>
    using namespace std;
    int main()
    {
    char a[13];
    cin>>a;
    int sum=0;
    for(int i=0,j=1;i<12;i++)
    {
    if(a[i]!='-')
    {
    sum+=(a[i]-'0')*j;
    j++;
    }

    }
    if(sum%11==10)
    {
    if(a[12]-'0'=='X'-'0')
    {
    cout<<"Right"<<endl;
    }
    else
    {
    for(int y=0;y<12;y++)
    {
    cout<<a[y];
    }
    cout<<'X'<<endl;
    }
    }
    else{
    if(a[12]-'0'==sum%11)
    {
    cout<<"Right"<<endl;
    }
    else
    {
    for(int y=0;y<12;y++)
    {
    cout<<a[y];
    }
    cout<<sum%11<<endl;
    }
    }
    return 0;
    }

    不得不说这个 用反斜杠b还过不了 奇怪了

  • 0
    @ 2019-01-11 18:20:02

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<vector>
    using namespace std;
    char a[14],b[14],t1,t2;
    int t22=0;
    int main()
    {
    scanf("%c-%c%c%c-%c%c%c%c%c-%c",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9],&t1);
    for(int i=1;i<=9;++i)
    t22+=(a[i]-'0')*i;
    t2=t22%11+'0';
    if(t2=='0'+10)
    t2='X';
    if(t1==t2)
    {
    cout<<"Right";
    return 0;
    }
    cout<<a[1]<<'-'<<a[2]<<a[3]<<a[4]<<'-'<<a[5]<<a[6]<<a[7]<<a[8]<<a[9]<<'-'<<t2;
    return 0;
    }

  • 0
    @ 2018-10-26 20:54:36

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

    char a[14],b[14],t1,t2;
    int t22=0;

    int main()
    {
    scanf("%c-%c%c%c-%c%c%c%c%c-%c",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9],&t1);
    for(int i=1;i<=9;++i)
    t22+=(a[i]-'0')*i;
    t2=t22%11+'0';
    if(t2=='0'+10)t2='X';
    if(t1==t2)
    {
    cout<<"Right";
    return 0;
    }
    cout<<a[1]<<'-'<<a[2]<<a[3]<<a[4]<<'-'<<a[5]<<a[6]<<a[7]<<a[8]<<a[9]<<'-'<<t2;
    return 0;
    }

  • 0
    @ 2018-09-24 12:26:40
    
    #include<iostream>
    using namespace std;
    char s[14];
    int main()
    {
        cin>>s+1;
        long long a=0;
        a+=((s[1]-'0')*1)%11;
        a+=((s[3]-'0')*2)%11;
        a+=((s[4]-'0')*3)%11;
        a+=((s[5]-'0')*4)%11;
        a+=((s[7]-'0')*5)%11;
        a+=((s[8]-'0')*6)%11;
        a+=((s[9]-'0')*7)%11;
        a+=((s[10]-'0')*8)%11;
        a+=((s[11]-'0')*9)%11;
        a%=11;
        if(a==s[13]-'0'||a==10&&s[13]=='X')
        {
            cout<<"Right";
            return 0;
        }
        for(int i=1;i<=12;i++)
            cout<<s[i];
        if(a==10)
            cout<<'X';
        else cout<<a;
        return 0;
    }
    
    

    暴力AC

  • 0
    @ 2018-07-12 16:19:17

    python版本
    if name == '__main__':
    string = input().split('-')
    s = ''
    sign = 0
    sign1 = ''
    for i in string:
    s += i
    for k in range(9):
    sign += int(s[k])*(k+1)
    if sign % 11 == 10:
    sign1 = 'X'
    else:
    sign1 = str(sign % 11)
    # print(sign1)
    if s[9] == sign1:
    print('Right')
    else:
    print(s[0] + '-' + s[1:4] + '-' + s[4:9] + '-' + sign1)

  • 0
    @ 2018-07-11 21:59:14

    思路:
    1、读入字符串
    2、将除了识别码之外的所有数字符号转化并保存进一个数组当中
    3、将数组内的数字进行乘法并累加
    4、判断并给出正确的ISBN码
    /* This is a program to give the right ISBN. */

    #include <stdio.h>
    #include <string.h>
    int check (int a[9])
    {
    int i, sum = 0, mod;
    for (i = 0; i < 9; i++)
    {
    sum += a[i]*(i + 1);
    }
    mod = sum % 11;
    return mod;
    }
    int main()
    {
    char ch[13];
    int i, j, r, mod, a[9];
    for (i = 0; i < 13; i++)
    {
    scanf ("%c", &ch[i]);
    }
    for (j = 0, i = 0; j < 12; j++)
    {
    r = ch[j] - '0';
    if (r >= 0 && r <= 9)
    {
    a[i] = r;
    i++;
    }
    }
    mod = check (a);
    if (ch[12] - '0' == mod || (mod == 10 && ch[12] == 'X')){
    printf ("Right");
    }else
    {
    if (mod == 10)
    {
    ch[12] = 'X';
    }else
    {
    ch[12] = '0' + mod;
    }
    for (i = 0; i < 13; i++)
    {
    printf ("%c", ch[i]);
    }
    }
    return 0;
    }

  • 0
    @ 2018-07-05 00:41:43

    #include <iostream>
    using namespace std;
    int main(int argc, char** argv)
    {
    string A;
    int a;
    cin>>A;
    a=(A[0]-'0')+(A[2]-'0')*2+(A[3]-'0')*3+(A[4]-'0')*4+(A[6]-'0')*5+(A[7]-'0')*6+(A[8]-'0')*7+(A[9]-'0')*8+(A[10]-'0')*9;
    a=a%11;
    if(A[12]=='X')
    {
    if(a==10) cout<<"Right";
    else cout<<(A[0]-'0')<<'-'<<(A[2]-'0')<<(A[3]-'0')<<(A[4]-'0')<<'-'<<(A[6]-'0')<<(A[7]-'0')<<(A[8]-'0')<<(A[9]-'0')<<(A[10]-'0')<<'-'<<a;
    }
    else
    {
    if(a==(A[12]-'0')) cout<<"Right";
    else
    {
    if(a==10) cout<<(A[0]-'0')<<'-'<<(A[2]-'0')<<(A[3]-'0')<<(A[4]-'0')<<'-'<<(A[6]-'0')<<(A[7]-'0')<<(A[8]-'0')<<(A[9]-'0')<<(A[10]-'0')<<'-'<<'X';
    else cout<<(A[0]-'0')<<'-'<<(A[2]-'0')<<(A[3]-'0')<<(A[4]-'0')<<'-'<<(A[6]-'0')<<(A[7]-'0')<<(A[8]-'0')<<(A[9]-'0')<<(A[10]-'0')<<'-'<<a;
    }
    }
    return 0;
    }

  • 0
    @ 2018-04-14 17:29:36

    其实就是纯模拟,但有很多关键点。以下代码中打“!”表示要重点注意
    ```cpp
    #include<bits/stdc++.h>
    using namespace std ;
    //Vijos P1484

    int x[9] , n , ans ;
    char y , a , b ;
    int main()
    {
    for (int i = 1 ; i <= 5 ; i += 2)//分成三段读入
    {
    for (int j = 0 ; j < i ; j ++)
    {
    cin >> a ;//!一定要用字符型一个一个读入,不然程序会将一整段当成一个整数
    x[n] = a - '0' ;//处理
    ans += x[n] * (n + 1) ;//!一定是(n+1)
    n ++ ;
    }
    cin >> b ;//间隔符
    }
    cin >> y ;//!y是字符型,因为会有“X”
    ans = ans % 11 ;//!对11取余,题目要求
    n = 0 ;
    if ((y - '0' == ans) || ((y == 'X') && (ans == 10)))//!y所表示的数字,或两者都是10
    printf("Right") ;
    else
    {
    for (int i = 1 ; i <= 5 ; i += 2)//分三段
    {
    for (int j = 0 ; j < i ; j ++)
    {
    cout << x[n] ;
    n ++ ;
    }
    cout << b ;
    }
    if (ans == 10)
    cout << "X" ;
    else
    cout << ans ;
    }
    return 0 ;
    }
    ```

  • 0
    @ 2018-02-18 20:17:32

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

    char fuc(int k)
    { if(k==10) return 'X';
    else return k+'0';
    }
    int main()
    {
    string str;
    cin>>str;
    int sum=0,j=1;
    for(int i=0;i!=11;++i)
    {
    if(str[i]=='-') continue;
    else
    {
    sum+=(str[i]-48)*j;
    ++j;
    }
    }
    int k=sum%11;
    char n=fuc(k);
    if(n==str[12]) cout<<"Right"<<endl;
    else
    {
    str[12]=n;
    cout<<str;
    }
    }

  • 0
    @ 2018-02-09 11:19:47
    #include<iostream>
    
    using namespace std;
    
    int main(){
        int cnt,ans;    
        int pro;
        char s[15];
        cin>>s;
        cnt=1;
        ans=0;
        for(int i=0;i<=10;i++)
            if(s[i]!='-') ans=(ans+cnt*(s[i]-'0'))%11,cnt++;
        if (s[12]=='X')
            pro=10;
        else
            pro=s[12]-'0';
        if (pro==ans) cout<<"Right";
        else{
            if (ans==10)
                s[12]='X';
            else s[12]=ans+'0';
            cout<<s;
        }
        
        return 0;
    }
    
  • 0
    @ 2017-12-19 16:39:15

    //简单AC,string类

    #include <iostream>
    #include<string>
    using namespace std;
    int main()
    {
    string a;
    int add,n=1;
    cin>>a;
    int s=a.size()-1 ;
    for(int i=0;i<s;i++)
    {
    if(a[i]!='-') add+=(a[i]-'0')* n++;
    }
    if(add%11==(a[s]-'0')||( add%11==10 && a[s]=='X'))cout<<"Right"<<endl;
    else if(add%11==10)cout<<"X"<<endl;
    else cout<<add%11<<endl;

    return 0;
    }

  • 0
    @ 2017-11-23 13:51:47
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    char s[13]= {'\0'};
    int main() {
        int ans = 0;
        char end;
        int i,j;
        scanf("%s", s);
        end=s[12];
        ans=s[0]-'0';
        for(i=2 ; i<=4 ; i++) ans+=(s[i]-'0')*i;
        for(j=6 ; j<=10 ; j++) ans+=(s[j]-'0')*(j-1);
        ans=ans%11;
        if(ans == 10){
            if(end == 'X') printf("Right");
            else{
                for(i=0 ; i<=11 ; i++){
                    printf("%c",s[i]);
                }
                printf("X");
            }
            return 0;
        }
        end -= '0';
        if(ans == end) printf("Right");
        else{
            for(i=0 ; i<=11 ; i++){
                printf("%c",s[i]);
            }
            printf("%d",ans);
        }
        return 0;
    }
    
  • 0
    @ 2017-11-20 18:47:40

    //Java版的代码,注释的很全了

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {
    int leap=1; //用于每次累乘的自加数。
    int sum=0; //加起来的和
    Scanner sc=new Scanner(System.in);
    String s=sc.next();
    for (int i = 0; i < s.length()-1; i++) { //用于计算出累加的和
    if(s.charAt(i)!='-'){

    sum+=Integer.parseInt(String.valueOf(s.charAt(i)))*leap;
    leap++;
    }
    }
    sum=sum%11; //计算出识别码
    if(sum!=10) //
    {
    //虽然说进入这个循环之后正确的识别码不可能是X,但是输入的可能是X。所以要一次判断。
    if(String.valueOf(s.charAt(s.length()-1)).equals("X"))
    {
    for (int i = 0; i <s.length()-1; i++) {
    System.out.print(s.charAt(i));
    }
    System.out.println(sum);

    }

    //如果输入的识别码是X,则会抛出NumberFormatException:数字格式化异常。
    //所以要在此做一个判断,判断最后一个是不是X,如果是则直接输出正确的识别码。
    else if(sum==Integer.parseInt(String.valueOf(s.charAt(s.length()-1))))
    //如果计算得到的识别码和原识别码相同
    System.out.println("Right");
    else //如果不同则输出计算得出的识别码
    {
    for (int i = 0; i <s.length()-1; i++) {
    System.out.print(s.charAt(i));
    }
    System.out.println(sum);
    }
    }

    else //如果计算出的识别码等于10
    {
    if(String.valueOf(s.charAt(s.length()-1)).equals("X"))
    //和原识别码一样都是X
    System.out.println("Right");
    else
    { //原识别码不是X
    for (int i = 0; i < s.length()-1; i++) {
    System.out.print(s.charAt(i));
    }
    System.out.println("X");
    }
    }

    }

    }

  • 0
    @ 2017-11-05 19:01:23

    #include<cstdlib>
    #include<cstdio>
    #include<iostream>
    using namespace std;
    int i,j,n,k,h,l=10;
    int a[15];
    char s[14];
    int main()
    {
    // freopen("isbn.in","r",stdin);
    // freopen("isbn.out","w",stdout);

    scanf("%s",s);
    for(i=0;i<=12;i++)
    {
    if(s[i]!='-' && s[i]!='X' )
    a[++k]=s[i]-48;
    if(s[i]=='X')
    a[++k]=10;
    }
    for(i=1;i<=9;i++)
    h=h+a[i]*i;
    j=h%11;
    if(a[10]==j) printf("Right");
    else
    {
    for(i=0;i<=11;i++)
    {
    if(s[i]!='-') printf("%d",s[i]-48);
    else printf("-");
    }

    if(j==l) printf("X");
    else printf("%d",j);
    }
    return 0;
    }

  • 0
    @ 2017-11-01 16:39:51

    肯定没人用 regex

    #include <iostream>
    #include <regex>
    #include <string>
    #include <sstream>
    
    int main()
    {
      std::string line;
      std::getline(std::cin, line);
      const auto regex = std::regex(R"((\d)-(\d{3})-(\d{5})-([\dX])\r?)");
      const auto number = std::regex_replace(line, regex, "$1$2$3");
      const auto fix = std::regex_replace(line, regex, "$4");
      int f;
      if (fix == "X") {
        f = 10;
      } else {
        std::stringstream ss;
        ss << fix;
        ss >> f;
      }
      int i = 1;
      int right = 0;
      for (const auto& c : number) {
        right += (c - '0') * i;
        ++i;
      }
      right %= 11;
      if (right == f) {
        std::cout << "Right" << std::endl;
      } else {
        std::cout << std::regex_replace(line, regex, "$1-$2-$3-");
        if (right == 10)
          std::cout << "X";
        else
          std::cout << right;
        std::cout << std::endl;
      }
    }
    

信息

ID
1484
难度
5
分类
字符串 点击显示
标签
递交数
14462
已通过
4955
通过率
34%
被复制
33
上传者