code1 14分
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n, flag=0, sum,count;
cin >> n;
string s, t = "10X98765432";;
int weight[17] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
for (int i = 0; i < n; i++) {
count=0,sum = 0;
cin >> s;
for (int j = 0; j <= 16; j++) {
if (!isdigit(s[j])) {
flag++;
cout << s << endl;
break;
}
sum += (s[j] - '0')*weight[j];
}
sum = sum % 11;
if (count==0&&t[sum] != s[17]) {
cout << s << endl;
flag++;
}
}
if (flag == 0) cout << "All passed";
return 0;
}
bug:1.在于没有分清楚,flag,sum,count三者代表的参数,哪些初始化,哪些不需要。
2.count 的作用在于判断之前17位数字是否输出过。
code2 15分
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n, flag=0, sum,count;
cin >> n;
string s, t = "10X98765432";;
int weight[17] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
for (int i = 0; i < n; i++) {
count=0,sum = 0;
cin >> s;
for (int j = 0; j <= 16; j++) {
if (!isdigit(s[j])) {
flag++;
count=1;
cout << s << endl;
break;
}
sum += (s[j] - '0')*weight[j];
}
sum = sum % 11;
if (count==0&&t[sum] != s[17]) {
cout << s << endl;
flag++;
}
}
if (flag == 0) cout << "All passed";
return 0;
}