You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无循环实现ISBN-10第10位校验位计算错误排查求助

解决ISBN-10校验位计算问题(输入013601267时结果错误)

Hey there! Let's fix that ISBN-10 check digit calculation issue you're having. When you input 013601267, your code outputs 5 instead of the correct 1—let's break down what's wrong and how to fix it using getline without loops.

首先明确ISBN-10的校验规则

  • 取ISBN的前9位数字;
  • 每位数字分别乘以从10递减到2的权重(即第1位×10,第2位×9,……,第9位×2);
  • 将所有乘积求和;
  • 计算 (11 - (总和 % 11)) % 11 得到校验位,如果结果为10,用X代替数字。

问题根源与修正代码

你的错误大概率是权重应用顺序错误,或者没有正确处理字符串输入的每一位。下面是使用getline且无循环的修正实现:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string isbn_prefix;
    cout << "Enter the first 9 digits of ISBN-10: ";
    getline(cin, isbn_prefix);

    // 验证输入长度是否为9位
    if (isbn_prefix.length() != 9) {
        cout << "Error: Please enter exactly 9 digits." << endl;
        return 1;
    }

    // 无循环计算加权总和
    int sum = (isbn_prefix[0] - '0') * 10 +
              (isbn_prefix[1] - '0') * 9 +
              (isbn_prefix[2] - '0') * 8 +
              (isbn_prefix[3] - '0') * 7 +
              (isbn_prefix[4] - '0') * 6 +
              (isbn_prefix[5] - '0') * 5 +
              (isbn_prefix[6] - '0') * 4 +
              (isbn_prefix[7] - '0') * 3 +
              (isbn_prefix[8] - '0') * 2;

    int check_digit = (11 - (sum % 11)) % 11;

    cout << "Check digit: ";
    if (check_digit == 10) {
        cout << 'X';
    } else {
        cout << check_digit;
    }
    cout << endl;

    return 0;
}

关键说明

  • 使用getline可以完整捕获输入字符串,包括开头的0(这对ISBN的有效性很重要);
  • 通过字符 - '0'将字符串中的字符转换为对应整数(ASCII码中数字是连续排列的,这个转换方式有效);
  • 正确应用了权重顺序:从10到2依次乘以每位数字,这是修正错误输出的核心;
  • 最终的校验位计算(11 - (sum % 11)) % 11处理了总和模11等于0的边界情况(此时校验位为0而非11)。

测试验证

输入013601267时:

  • 总和 = 0×10 +1×9 +3×8 +6×7 +0×6 +1×5 +2×4 +6×3 +7×2 = 0+9+24+42+0+5+8+18+14 = 120
  • 120 %11 = 10
  • (11-10)%11 =1 → 这正是正确的校验位。

内容的提问来源于stack exchange,提问作者Luigi Campbell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:02:42