ISBN
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 566 Accepted Submission(s): 172
Problem 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 '?'.
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
Sample Output
Source
题目分析 :
水题, 不错还是 WA 了2次 , 很郁闷 , 主要是没有注意到 题目描述中点一句话 :
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.
这句话是关键 , 意思就是 除了最后一个数字 可能是 X 外 其他的都只能为 0-9的数字.
代码如下 :
/*
Coded By : MiYu
Link : http://www.cnblogs.com/MiYu || http://www.cppblog.com/MiYu
Author By : MiYu
Test : 1
Program : 2714
*/
//#pragma warning( disable:4789 )
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int main ()
{
string str;
while ( cin >> str )
{
int N = str.size();
int sum = 0, ask = 0;
for ( int i = 1; i <= N; ++ i )
{
switch ( str[i-1] )
{
case 'X':
sum += (10-i+1) * 10; break;
case '?':
ask = 10-i+1; break;
default :
sum += (10-i+1) * (str[i-1] - '0'); break;
}
}
int pos = -1;
for ( int i = 0; i <= 10; ++ i )
{
if ( ( sum + ask * i ) % 11 == 0 )
{
pos = i;
break;
}
}
if ( ask != 1 && pos == 10 ) pos = -1; //没有这句代码 WA 2次 很郁闷.
if ( pos == 10 )
cout << 'X' << endl;
else
cout << pos << endl;
}
return 0;
}