ISBN号码

来源:互联网 发布:5g商用 知乎 编辑:程序博客网 时间:2024/04/30 02:40

题意

首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,...,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出你认为是正确的ISBN号码。


分析

先一个循环,算出它的识别码,再来判断它是否正确。


var
n:string;
l,l1,t,i,r:longint;
begin
    read(n);l:=length(n);
    l1:=0;t:=0;
    for i:=1 to l do
    begin
        if (n[i]>='0')and(n[i]<='9')and(l1<9) then
        begin
            l1:=l1+1;
            t:=t+(ord(n[i])-ord('0'))*l1;
        end;
    end;
    if (n[13]='X') then r:=10 else  r:=ord(n[13])-ord('0');
    t:=t mod 11;
    if r=t then write('Right') else
    begin
        write(copy(n,1,12));
        if t=10 then write('X') else write(t);
    end;
end.



0 0