Sicily 1129. ISBN
2016-01-07 00:39
477 查看
Time Limit: 10 secs, Memory Limit: 32 MB
Description
Practically every book that has been published during the past thirty years can be uniquely identified by its International Standard Book Number (ISBN). An example of an ISBN is
963-10-0604-2.
¨ An ISBN has 13 characters.
¨ Exactly three of the 13 characters are hyphens.
n The first (leftmost) character of the ISBN is not a hyphen.
n The next-to-last character of the ISBN is a hyphen.
n No two hyphens occupy adjacent positions.
¨ The last (rightmost) character is either a decimal digit (0..9) or an upper case X.
¨ The nine characters which are not in the rightmost position and are not hyphens are decimal digits.
The rightmost character is known as the check digit. It can actually be calculated from the other digits, according to the following steps (as each step is stated, it will be illustrated using the example 963-10-0604-2).
1. Assign numbers 10,9,8,… as the respective “weights” to the first, second, third, … digit:
Calculate the weighted sum by adding the products obtained by multiplying each of the first nine digits by its weight:
10*9 + 9*6 + 8*3 + 7*1 + 6*0 + 5*0 + 4*6 + 3*0 + 2*4 = 207.
The check digit is the smallest non-negative number that can be added to the weighted sum in order to get a number that is divisible by 11. The check digit is always in the range 0..10. If it is equal to 10, we denote it by an upper case X. In the given example, the check digit is 2, because 207 + 2 = 209, which is divisible by 11.
Input
The input file will contain one or more lines. Each line will contain the first 11 characters of an ISBN, starting in column 1. The input will not contain any white spaces, except for the end-of-line characters.
Output
In the output, each incomplete ISBN from the input will be completed: the third hyphen and the check digit will have been added. The output will contain one ISBN per line, starting at column 1.
Sample Input
0-07-109219
0-534-95054
963-10-0604
1-890880-01
Sample Output
0-07-109219-6
0-534-95054-X
963-10-0604-2
1-890880-01-9
O(∩_∩)O~~ Just do it!
Description
Practically every book that has been published during the past thirty years can be uniquely identified by its International Standard Book Number (ISBN). An example of an ISBN is
963-10-0604-2.
¨ An ISBN has 13 characters.
¨ Exactly three of the 13 characters are hyphens.
n The first (leftmost) character of the ISBN is not a hyphen.
n The next-to-last character of the ISBN is a hyphen.
n No two hyphens occupy adjacent positions.
¨ The last (rightmost) character is either a decimal digit (0..9) or an upper case X.
¨ The nine characters which are not in the rightmost position and are not hyphens are decimal digits.
The rightmost character is known as the check digit. It can actually be calculated from the other digits, according to the following steps (as each step is stated, it will be illustrated using the example 963-10-0604-2).
1. Assign numbers 10,9,8,… as the respective “weights” to the first, second, third, … digit:
Calculate the weighted sum by adding the products obtained by multiplying each of the first nine digits by its weight:
10*9 + 9*6 + 8*3 + 7*1 + 6*0 + 5*0 + 4*6 + 3*0 + 2*4 = 207.
The check digit is the smallest non-negative number that can be added to the weighted sum in order to get a number that is divisible by 11. The check digit is always in the range 0..10. If it is equal to 10, we denote it by an upper case X. In the given example, the check digit is 2, because 207 + 2 = 209, which is divisible by 11.
Input
The input file will contain one or more lines. Each line will contain the first 11 characters of an ISBN, starting in column 1. The input will not contain any white spaces, except for the end-of-line characters.
Output
In the output, each incomplete ISBN from the input will be completed: the third hyphen and the check digit will have been added. The output will contain one ISBN per line, starting at column 1.
Sample Input
0-07-109219
0-534-95054
963-10-0604
1-890880-01
Sample Output
0-07-109219-6
0-534-95054-X
963-10-0604-2
1-890880-01-9
O(∩_∩)O~~ Just do it!
#include <iostream> #include <string> using namespace std; int main() { string str; int digit, sum; while (cin >> str) { sum = 0; digit = 10; /// for (int i = 0; i < str.size(); i++) if (str[i] != '-') sum += (str[i] - 48) * (digit--); /// int i = sum; while (i % 11 != 0) i++; /// if (i - sum > 9) cout << str << "-X" << endl; else cout << str << "-" << i - sum << endl; } return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性