每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔符后的五位数字代表该书在该出版社的编号;最后一位为识别码。
识别码的计算方法如下:
首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,...,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。
你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出你认为是正确的ISBN号码。
输入格式:
输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN的格式要求)。
用gets()输入字符串
输出格式:
输出共一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。
输入样例1:
0-123-41562-4
输出样例1:
Right
输入样例2:
0-123-41562-7
输出样例2:
0-123-41562-4
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string str;
while(cin>>str)
{
int sum=(str[0]-'0')*1+(str[2]-'0')*2+(str[3]-'0')*3+(str[4]-'0')*4+(str[6]-'0')*5+(str[7]-'0')*6+(str[8]-'0')*7+(str[9]-'0')*8+(str[10]-'0')*9;
int result=sum%11;
char c;
if(result==10) c='X';
else c=result+'0';
if(str[12]==c) cout<<"Right"<<endl;
else
{
cout<<str[0]<<str[1]<<str[2]<<str[3]<<str[4]<<str[5]<<str[6]<<str[7]<<str[8]<<str[9]<<str[10]<<str[11]<<c<<endl;
}
}
return 0;
}
#include <stdio.h>
int main()
{
char ISBN[14];
while (gets_s(ISBN))
{
int sum = (ISBN[0] - '0') * 1 + (ISBN[2] - '0') * 2 + (ISBN[3] - '0') * 3 + (ISBN[4] - '0') * 4 + (ISBN[6] - '0') * 5 + (ISBN[7] - '0') * 6 + (ISBN[8] - '0') * 7 + (ISBN[9] - '0') * 8 + (ISBN[10] - '0') * 9;
int result = sum % 11;
char c;
if (result == 10) c = 'X';
else c = result + '0';
if (ISBN[12] == c)
printf("Right\n");
else
{
for (int i = 0; i < 12; ++i)
printf("%c", ISBN[i]);
printf("%c\n", c);
}
}
return 0;
}