PTA 1031 查验身份证 (15 分)

文章目录

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值