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
15688?111X
Sample Output
1
题意:定义一种数叫做 ISBN,其有十个数字,令第 i 位乘以 i,将乘后的值相加,若等于 11 的整数倍则有效,现在给出一个缺省的 ISBN,其中 X 代表 10,?为未知数,如果这个数是 ISBN 数,求 ?的值,如果取不到任何值使得给出的数为 ISBN 数,则输出 -1
思路:直接计算现有的值的和,然后枚举 ?的值即可,需要注意的是,如果末尾数为 10,则输出 X,其他位数为 10 时,输出 -1
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
LL dp[1000005];
map<char,int> mp;
void init() {
mp['0']=0;
mp['1']=1;
mp['2']=2;
mp['3']=3;
mp['4']=4;
mp['5']=5;
mp['6']=6;
mp['7']=7;
mp['8']=8;
mp['9']=9;
mp['X']=10;
}
int main() {
init();
string str;
cin>>str;
int x=-1;
int sum=0;
for(int i=0; i<10; ++i) {
if(str[i]!='?')
sum+=mp[str[i]]*(10-i);
else
x=10-i;
}
int len=9;
if(x==1)
len=10;
for(int i=0; i<=len; ++i) {
int temp=sum+x*i;
if(temp%11==0) {
if(i==10)
cout<<'X'<<endl;
else
cout<<i<<endl;
return 0;
}
}
printf("-1\n");
return 0;
}