SICILY 1129. ISBN
来源:互联网 发布:大华监控怎么连接网络 编辑:程序博客网 时间:2024/04/29 04:51
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
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 case X. In the given example, the check digit is 2, because 207 + 2 = 209, which is divisible by 11.
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.
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.
0-07-1092190-534-95054963-10-06041-890880-01
0-07-109219-60-534-95054-X963-10-0604-21-890880-01-9
#include<iostream>
#include<cstring>
char a[101];
using namespace std;
int main()
{
while(cin>>a)
{
int sum=0;
int b=0;
int c=10;
int n=strlen(a);
for(int i=0;i<n;i++)
{
if(a[i]=='-') continue; //或a[i]-'0'<0
else sum=sum+(a[i]-'0')*c,c--;
}
for(int j=sum;j>0;j++)
{
if(j%11==0) {
if(j==sum){
b=0;break;}
else {
b=j-sum;break;}}
else continue;
}
if(b!=10) cout<<a<<"-"<<b<<endl;
if(b==10) cout<<a<<"-"<<"X"<<endl; //注意为大写
memset(a,0,101);
}
return 0;
}
- sicily--1129. ISBN
- Sicily 1129. ISBN
- SICILY 1129. ISBN
- sicily 1129. ISBN
- [sicily]1129. ISBN
- Sicily 1129. ISBN
- sicily ISBN
- ISBN Sicily
- Sicily ISBN
- 1129. ISBN
- ISBN
- ISBN
- isbn
- ISBN
- ISBN
- ISBN
- 【模拟】ISBN
- ISBN号码
- 关系型数据库与面向对象的冲突
- AppleScript学习笔记(六)处理程序Handler
- Avoid empty catch blocks
- Be specific in throws clause
- python安装及插件安装
- SICILY 1129. ISBN
- Beware of unknown root causes
- 傀儡进程内存Dump
- linux下fcitx输入法安装
- hdu 3954 Level up 线段树特殊更新节点
- Android binder -- Binder机制编程
- Activity的几种启动方式
- HDU 1969
- XCode5 真机调试及发布应用