[PAT-A 1035]Password

在这里插入图片描述
在这里插入图片描述
题目大意:
给定N个用户的姓名和密码,现在需要对密码中相应的易混淆的字符替换成别的字符。统计一组输入中需要有几个密码需要替换,输出需要替换的个数个相应的用户名以及替换过后的密码,如果没有需要替换的,则输出对应提示。

思路:
1.定义结构体Node,其中保存输出的用户名,密码,和bool类型变量ischange来记录是否被修改,初值为flase
2.定义int型变量cnt,初值为0,保存替换过的密码的个数。
3.对每一个输出的结构体变量,遍历密码字符串的每一个字符,如果存在给定的需要替换的字符,进行替换,同时令ischange=true。每一个密码遍历结束之后,检查ischange变量,如果为true,证明已经被修改过,cnt++
4.输出。
1)如果cnt==0,表示没有,输出对应提示(注意有一个例子和有多个例子的提示语句不同)。
2)如果cnt!=0,遍历结构体数组,对其中所有ischange为true的项输出。

AC代码:

//PAT_A 1035
#include<cstdio>
#include<cstring>
using namespace std;
struct node {
	char username[20];
	char password[20];
	bool ischange;//ischange==true表示已经修改
}T[1005];
int main() {
	int n, cnt = 0;
	(void)scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		(void)scanf("%s %s", T[i].username, T[i].password);
		T[i].ischange = false;
		int len = strlen(T[i].password);
		for (int j = 0; j < len; j++) {
			if (T[i].password[j] == '1') {
				T[i].password[j] = '@';
				T[i].ischange = true;
			}
			else if (T[i].password[j] == '0') {
				T[i].password[j] = '%';
				T[i].ischange = true;
			}
			else if (T[i].password[j] == 'l') {
				T[i].password[j] = 'L';
				T[i].ischange = true;
			}
			else if (T[i].password[j] == 'O') {
				T[i].password[j] = 'o';
				T[i].ischange = true;
			}
		}
		if (T[i].ischange)cnt++;
	}
	if (cnt == 0) {//没有密码需要修改
		if (n == 1)printf("There is 1 account and no account is modified");
		else printf("There are %d accounts and no account is modified", n);
	}
	else {
		printf("%d\n", cnt);
		for (int i = 0; i < n; i++) {
			if (T[i].ischange == true)
				printf("%s %s\n", T[i].username, T[i].password);
		}
	}
	return 0;
}
发布了101 篇原创文章 · 获赞 1 · 访问量 2990

猜你喜欢

转载自blog.csdn.net/weixin_44699689/article/details/104147078