POJ 2190 ISBN(我的水题之路——%11,状况很多)

ISBN
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13114   Accepted: 4571

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

Source

USACO 2003 Fall Orange

一种数,如 0201103311是ISBN数,取值方法为 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 = 55. 如果这个值可以整除11,则成立,现在取一列中一个为?,算出如果这个是ISBN数,?代表什么数。

直接计算现有值的总和,然后运算出缺少位数的值。

注意点:
1)如果取不到任何值使得这个数位ISBN数,就输出-1.(1WA)
2)如果是末尾数为10,则输出'X';其他位数也可能为10,则输出-1.
3)如果取值不到,需要在结果变量初始化为-1.(1WA)

代码(1AC 2WA):
#include <cstdio>
#include <cstdlib>

char str[15];

int main(void){
    int at, end;
    int i, j;
    int sum, result;

    while (scanf("%s", str) != EOF){
        sum = 0;
        for (i = 0; i < 10; i++){
            if (str[i] == '?'){
                at = 10 - i;
            }
            else{
                if (str[i] != 'X'){
                    sum += (str[i] - '0') * (10 - i);
                }
                else{
                    sum += 10;
                }
            }
        }
        end = (at == 1 ? 10 : 9);
        //printf("at%d, sum%d\n", at, sum);
        result = -1;
        for (i = 0; i <= end; i++){
            if ((sum + (i * at)) % 11 == 0){
                result = i;
                break;
            }
        }
        if (result != 10){
            printf("%d\n", result);
        }
        else if (result == 10 && at == 1){
            printf("X\n");
        }
        else{
            printf("-1\n");
        }
    }
    return 0;
}

你可能感兴趣的:(input,library,output,books,Numbers)