Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16456 | Accepted: 5790 |
Description
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
10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 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 '?'.
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
10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 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
Source
问题链接:POJ2190 HDU2714 ISBN。
问题简述:参见上文。
问题分析:
单纯的计算问题。需要注意以下几点:
1.如果是末尾数,则输出为‘X’;
2.如果能找到对应的值则输出;
3.如果找不到对应的值则输出-1。
程序说明:(略)
AC的C++语言程序如下:
/* POJ2190 HDU2714 ISBN */
#include <iostream>
#include <string>
using namespace std;
const int N = 10;
const int MOD = 11;
int main()
{
string s;
int sum, index, ans;
while(cin >> s) {
sum = 0;
index = MOD;
for(int i=0; i<N; i++)
if(s[i] != '?')
if(s[i] != 'X')
sum += (N - i) * (s[i] - '0');
else
sum += N;
else if(s[i] == '?')
index = (N - i);
ans = -1;
for(int i=0; i<=N; i++)
if((sum + i * index) % MOD == 0 && i <= N - 1) {
cout << i << endl;
ans = 0;
break;
} else if((sum + i * index) % MOD == 0 && s[N - 1] == '?') {
cout << 'X' << endl;
ans = 0;
break;
}
if(ans)
cout << -1 << endl;
}
return 0;
}