当前位置:网站首页>P1055 [noip2008 popularization group] ISBN number

P1055 [noip2008 popularization group] ISBN number

2022-07-07 23:40:00 Yuesi

P1055 -NOIP2008 Popularization group -ISBN number

Every book officially published has a ISBN The number corresponds to ,ISBN Code includes 9 Digit number 、1 Bit ID and 3 Bit separator , The format is as follows x-xxx-xxxxx-x, Symbols - It's the separator ( The minus sign on the keyboard ), The last one is the identification number , for example 0-670-82162-4 It's a standard ISBN code .ISBN The first digit of the code indicates the language of publication of the book , for example 0 For English ; First separator - The next three figures represent the publisher , for example 670670 On behalf of Viking press ; The five digits after the second separator represent the number of the book in the publishing house ; The last digit is the identification number .

The identification code is calculated as follows :
Multiply the first number by 1 Multiply the last digit by 2…… And so on , Use the results mod 11, The remainder obtained is the identification code , If the remainder is 1010, The identification code is capital letters XX. for example ISBN number 0-670-82162-4 Identification code in 4 This is how I got : Yes 067082162 this 99 A digital , From left to right , Times, respectively, 1,2,…,9 And then sum up , namely 0×1+6×2+……+2×9=1580×1+6×2+……+2×9=158, And then take 158 mod 11 Result 4 As an identification number .

Your task is to write a program to judge the input ISBN Is the identification number correct , If correct , Only output Right; If the error , Then output what you think is correct ISBN number .

Input format
A character sequence , For a book ISBN number ( Make sure the input matches ISBN Format requirements for number ).

Output format
a line , If you type ISBN The ID of the number is correct , Then output Right, otherwise , In the prescribed form , Output correct ISBN number ( Include separator -).

I/o sample
Input #1
0-670-82162-4
Output #1
Right

Input #2
0-670-82162-0
Output #2
0-670-82162-4

explain / Tips
2008 First question of popularization group

 It's easy to get wrong :
%d What you read is not a number 
 Often make such common sense mistakes unintentionally 
#include<bits/stdc++.h>
using namespace std;
int main(){
    
	string a;
	cin>>a;//string General way of reading in 
	int sum=0,num=1;
	for(int i=0;i<13;i++){
    
		if(i!=1&&i!=5&&i!=11&&i!=12){
    // remove - And identification code 
			sum+=(a[i]-'0')*num;
			num++;
		}
	}
	sum=sum%11;
	if(sum==10){
    
		if(a[12]=='X'){
    
			printf("Right\n");
		}else{
    
			for(int i=0;i<12;i++){
    
				printf("%c",a[i]);
			}
			printf("X\n");
		}
	}else{
    
		if(sum==(a[12]-'0')){
    
			printf("Right\n");
		}else{
    
			for(int i=0;i<12;i++){
    
				printf("%c",a[i]);
			}
			printf("%d\n",sum);		
		}
	}
	return 0;
}

原网站

版权声明
本文为[Yuesi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130556374861.html