题解 | #[NOIP2008]ISBN号码#

[NOIP2008]ISBN号码

https://ac.nowcoder.com/acm/problem/16617

这道题根据题意,只要将除识别码外所有的数字提出,并同时乘以一个数,我们可以用一个变量来表示这个数。 唯一要注意的是当识别码为10的时候,用X表示。如果最后正确就输出Right 否则就输出前面的号码和正确的识别码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
    char c[100];
    int sum=0,i,k=1,m;
    fgets(c,100,stdin);
    for(i=0;c[i];i++)
        if(isdigit(c[i]))
        {
             sum=sum+(c[i]-'0')*k++;//k来表示要乘的数
            if(k==10)
                break;
        }
    if(c[i+2]-'0'==sum%11||c[i+2]==sum%11+78)//或前面判断一位识别码 后面判断识别码为10 是否为X
        cout << "Right" << endl;
    else
    {
        m=sum%11;
        if(m==10)
            c[i+2]='X';
        else
            c[i+2]=m+'0';
        cout << c ;
    }
}
import java.util.Scanner;
import java.lang.String;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] isbn=sc.nextLine().split("-");//分开
        int one=Integer.parseInt(isbn[0]);
        int two=Integer.parseInt(isbn[1]);
        int three=Integer.parseInt(isbn[2]);
        int count1=one*1;
        int count2=(two/100)*2+(two%100/10)*3+(two%10)*4;
        int count3=(three/10000)*5+(three%10000/1000)*6+(three%1000/100)*7+(three%100/10)*8+(three%10)*9;
        int count=(count1+count2+count3)%11;
        if(count==10)
        {
            if(isbn[3].equals("X"))
            {
                 System.out.println("Right");
            }
            else
            {
                System.out.println(one+"-"+two+"-"+three+"-X");
            }
        }
        else
        {
            if(isbn[3].equals(String.valueOf(count)))//String.valueOf 转换成字符串
            {
                System.out.println("Right");
            }
            else
            {
                System.out.println(one+"-"+two+"-"+three+"-"+count);
            }
        }
    }
}
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务