1000. ISBN

18 篇文章 0 订阅
13 篇文章 0 订阅

Time Limit: 10sec Memory Limit:32MB
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:

Digit 9 6 3 1 0 0 6 0 4
Weight 10 9 8 7 6 5 4 3 2

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

  2. 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
Copy sample input to clipboard
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

我的代码

// Problem#: 19149
// Submission#: 4852922
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<string>
#define len 11
using namespace std;
int main() {
    string s;
    while(cin>>s) {
        int weight = 10;
        int sum = 0;
        for (int i = 0; i < len; i++) {
            if (s[i] == '-') continue;
            sum += (s[i]- '0') * weight;
            weight--;
        }
        int check = (11 - (sum % 11)) % 11;
        s += "-";
        if (check == 10) s+= "X";
        else s += (check + '0');
        cout << s << endl; 
    }
}                                 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值