sicily 1129. ISBN

来源:互联网 发布:中国软件资讯网 编辑:程序博客网 时间:2024/05/02 04:41

1129. ISBN

Constraints

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 thecheck 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 example963-10-0604-2).

 1. Assign numbers 10,9,8,... as the respective "weights" to the first, second, third, ... digit:  

Digit

9

6

3

1

0

0

6

0

4

Weight

10

9

8

7

6

5

4

3

2


2. 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.


      3. 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 caseX. 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-1092190-534-95054963-10-06041-890880-01

Sample Output

0-07-109219-60-534-95054-X963-10-0604-21-890880-01-9

题目分析

根据isbn的前9位数字来获得校验码

简单的模拟题,要注意获得的权值和可能能直接整除11

不想去考虑太多,我直接从0到10遍历一遍


#include <iostream>int main(){  std::string isbn;  while (std::cin >> isbn) {    int num = 0;    int weight = 10;    for (int i = 0; i < isbn.length(); ++i) {      if (isbn[i] != '-') {        num += (isbn[i] - '0') * weight;        weight--;      }    }    int ans;    for (ans = 0; ans <= 10; ++ans)      if ((num+ans)%11 == 0)        break;    if (ans == 10)      isbn = isbn + '-' + 'X';    else      isbn = isbn + '-' + (char)('0'+ans);    std::cout << isbn << std::endl;  }}


0 0
原创粉丝点击