跳到正文

题解

题解:P16877 [GKS 2022 #C] New Password

I_use_Cpp约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月17日
篇幅
约 3 分钟
题目
P16877

版本

  1. v1当前

P16877 [GKS 2022 #C] New Password 题解

这题普普通通,考察<cctype>的使用。

分析

  • 先检查字符串是否包含大写字符,小写字符,数字以及#@&*中的一项,若不则添加Aa0#等字符
  • 长度不足补全,设添加字符数为 mm,那么公式 max⁡(0,7−(n+m))\max(0, 7 - (n + m))即为要添加的字符个数,不足补 0。

接着输出。

代码

C++
#include <iostream>
#include <cstdio>
#include <string>

bool hasup(std::string s) {
	for (char c : s) if (c >= 'A' && c <= 'Z') return true;
	return false;
}
bool hasnum(std::string s) {
	for (char c : s) if (c >= '0' && c <= '9') return true;
	return false;
}
bool haslo(std::string s) {
	for (char c : s) if (c >= 'a' && c <= 'z') return true;
	return false;
}
bool hasp(std::string s) {
	for (char c : s) if (c == '#' || c == '@' || c == '&' || c == '*') return true;
	return false;
}
std::string a(std::string s) {
	std::string res;
	if (!hasup(s)) res += 'A';
	if (!hasnum(s)) res += '0';
	if (!haslo(s)) res += 'a';
	if (!hasp(s)) res += '#';
	return res; 
}
int main() {
	int T,i=1;
	std::cin >> T;
	while (T--) {
		int n; std::cin >> n;
		std::string s; std::cin >> s;
		std::string add = a(s);
		std::printf("Case #%d: %s\n", i, std::string(s + add + std::string(std::max(0, 7 - (n + (int)add.size())),'0')).c_str());
		i++;
	}
	// 神秘操作
	for (int i = 0; i < 1e10; ++i) {
	    int* p = new int[1000];
	    __asm__ volatile("" : : "r"(p) : "memory");
	    delete[] p;
	}
}
原文