ISBN
Farmer John’s cows enjoy reading books, and FJ has discovered that his cows produce more milk when they read books of a somewhat intellectual nature. He decides to update the barn library to replace all of the cheap romance novels with textbooks on algorithms and mathematics. Unfortunately, a shipment of these new books has fallen in the mud and their ISBN numbers are now hard to read
An ISBN (International Standard Book Number) is a ten digit code that uniquely identifies a book. The first nine digits represent the book and the last digit is used to make sure the ISBN is correct. To verify that an ISBN number is correct, you calculate a sum that is 10 times the first digit plus 9 times the second digit plus 8 times the third digit … all the way until you add 1 times the last digit. If the final number leaves no remainder when divided by 11, the code is a valid ISBN
For example 0201103311 is a valid ISBN, since
100 + 92 + 80 + 71 + 61 + 50 + 43 + 33 + 21 + 11 = 55
Each of the first nine digits can take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to ten; this is done by writing the last digit as X. For example, 156881111X is a valid ISBN number
Your task is to fill in the missing digit from a given ISBN number where the missing digit is represented as ‘?’.
Input
- Line 1: A single line with a ten digit ISBN number that contains ‘?’ in a single position
Output
- Line 1: The missing digit (0…9 or X). Output -1 if there is no acceptable digit for the position marked ‘?’ that gives a valid ISBN.
Sample Input
15688?111X
Sample Output
1
题意: 给10个数字,前9个范围都是0-9,最后一个可能会是10,如果是10,则用X来表示,然后有一个数字遭到了破坏,10个数字的和可以被11整除,让你求出这个数,被破坏的数用’?'表示。
这个题应该算简单的,但是我错了好几次,后来看了别人的代码,发现原来不止一组数据。。。我似乎在题目中并没有看到这个提示。
AC代码
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
char s[12];
while(scanf("%s", s) != EOF) {
int sum = 0, flag;
for(int i = 0; i < 10; i++) {
if('0' <= s[i] && s[i] <= '9') {
sum += (s[i] - '0') * (10 - i);
} else if(s[i] == '?') flag = 10 - i;
else if(s[i] == 'X') sum += 10;
}
int i;
for(i = 0; i <= 10; i++)
if((sum + flag * i) % 11 == 0) {
break;
}
if(i == 10) {
if(flag != 1) printf("-1\n");
else printf("X\n");
} else printf("%d\n", i);
}
return 0;
}
End