题目:
给定一个9位数字的ISBN,求其校验位。ISBN格式为2-02-033598,校验位的计算方法如下:从左到右依次将各位数字乘10,9,8,……,2,求出其和S,作模运算得M=S mod 11。若11-M在1和9之间,校验位即为该数字;若11-M等于10,校验位为X;11-M等于11,校验位为0。输出添加校验位的ISBN,如2-02-033598-0。
输入:2-02-033598
输出
2-02-033598-0
#include<iostream>
#include<string>
using namespace std;
int mp[10] = { 10,9,8,7,6,5,4,3,2 };
int main() {
string str;
cin >> str;
int cnt = 0,x=0;
for (int i = 0; i < str.length(); i++) {
if (str[i] >= '0'&&str[i] <= '9') {
x += (str[i] - '0')*mp[cnt++];
}
cout << str[i];
}
x =11 - x % 11;
if (x >= 1 && x <= 9)
cout << '-' << x;
else if (x == 10)
cout << '-' << 'X';
else if (x == 11)
cout << '-' << '0';
system("pause");
return 0;
}