CCF认证真题-(201312-2)-ISBN号码(模拟)
思路:
可以用string类型变量直接存字符串,根据题目要求确定参与计算的九个数字对应下标,用一个check变量存给定字符串的最后一位转换的数字(注意check可能是X),用sum变量存计算好的结果. 将sum % 11和check比较,如果一致输出Right,不一致将字符串最后一位改为sum%11转换成的字符(注意如果是10要转换为X).
1 #include <iostream> 2 using namespace std; 3 4 int toInt(char ch); 5 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 cin.tie(0); 10 11 // 0-670-8216 2 - 4 12 // 0123456789 10 11 12 13 // 02346789 10 普通数字的下标 14 // 12345678 9 乘以的数字 15 string isbn; 16 cin >> isbn; 17 int check = toInt(isbn[12]); 18 int sum = toInt(isbn[0]) + toInt(isbn[2])*2 19 + toInt(isbn[3])*3 + toInt(isbn[4])*4 20 + toInt(isbn[6])*5 + toInt(isbn[7])*6 21 + toInt(isbn[8])*7 + toInt(isbn[9])*8 22 + toInt(isbn[10])*9; 23 24 if (sum % 11 == check) 25 cout << "Right" << endl; 26 else { 27 isbn[12] = (sum % 11 == 10 ? \'X\' : char(sum % 11 + \'0\')); 28 cout << isbn << endl; 29 } 30 return 0; 31 } 32 33 int toInt(char ch) { 34 return ch == \'X\' ? 10 : int(ch - \'0\'); 35 }
版权声明:本文为AntonLiu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。