POJ 2190 ISBN(我的水题之路——%11,状况很多)
2012-02-10 15:57
323 查看
ISBN
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
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):
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13114 | Accepted: 4571 |
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; }
相关文章推荐
- POJ 1207 The 3n+1 problem(我的水题之路——输入输出要保持原有顺序)
- POJ 1326 Mileage Bank(我的水题之路——模拟,四舍五入)
- POJ 1595 Prime Cuts(我的水题之路——素数取中间区域,准确定位)
- POJ 2109 Power of Cryptography(我的水题之路——k^n=p)
- POJ 2538 WERTYU(我的水题之路——键盘错位)
- POJ 2636 Electrical Outlets(我的水题之路——水,电器接头)
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(我的水题之路——加i个d后的第几个素数)
- POJ 3427 Ecology tax(我的水题之路——不同的理解,不同的AC)
- POJ 1046 Color Me Less(我的水题之路——坐标最小点)
- POJ 1247 Magnificent Meatballs(我的水题之路——数组两边求和)
- POJ 2027 No Brainer(我的水题之路——。。。)
- POJ 2136 Vertical Histogram(我的水题之路——字符统计表)
- POJ 2459 Feed Accounting(我的水题之路——英文题啊!!!)
- POJ 2551 Ones(我的水题之路——重点,末尾有几个1)
- POJ 2656 Unhappy Jinjin(我的水题之路——不开心的学习日)
- POJ 3438 Look and Say(我的水题之路——N个M的队列)
- POJ 1118 Lining Up(我的水题之路——斜率最多)
- POJ 2000 Glod Conins(我的水题之路——之后的N天N个金币,打表)
- POJ 2070 Filling Out the Team(我的水题之路——double比较)
- POJ 3030 Nasty Hacks(我的水题之路——比较大小)