题解:P1055 [NOIP2008 普及组] ISBN 号码
思路
简单字符串模拟。观察到「ISBN号码」长度较短,考虑直接模拟。
我们定义两个变量 - 就换到下一个字符,对于非字符 - 的数字字符就令
遍历完成后,我们对比变量 X 是
最后,我们输出 Right 或正确的「ISBN号码」即可,X。
时间复杂度为
实现
# include <iostream>
# include <cstring>
using namespace std;
int main(){
string isbn;
cin >> isbn;
int ans = 0,index = 0;
for (int base = 1;base <= 9;base++){ // base表示当前这一位要乘几
if (isbn[index] == '-') index++;
int num = isbn[index] - '0';
ans += base * num;
ans %= 11;
index++;
}if (isbn[isbn.size()-1] == 'X' && ans == 10){
cout << "Right";
}else if (ans == (isbn[isbn.size()-1] - '0')){
cout << "Right";
}else if (ans != 10){
cout << isbn.substr(0,isbn.size()-1) << ans;
}else{
cout << isbn.substr(0,isbn.size()-1) << "X";
}return 0;
}