跳到正文

题解

题解:P1603 斯诺登的密码

__Groyhj__QwQ约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
题目
P1603

版本

仅有当前版本

P1603 斯诺登的密码 题解

1. 思路分析

这就是一道思路简单的模拟字符串题,依照题中给的四个步骤模拟即可。

  1. 我们可以写 20 个 if 一一判断,把英文单词变成对应的数字。注意可能有的英语单词不是数词。
  2. 将这些数字平方后对 100100 取模。我们可以把这一步与第一步合并在一起写,即:把英文单词变成对应的数字平方后对 100100 取模的结果。
  3. 我们将这些获得的数字排列一下,注意获得的每个数字需要补零至两位,开头的零要省略,如:{0,4,9,25}→40925≠00040925≠04925\{0,4,9,25\} \to 40925 \ne 00040925 \ne 04925 。
  4. 就是将第三步求出来的数字最小化,用 sort 把获得的数字从小到大排序即可,不会用的可以去百度下,这里就不细讲了。

2. AC 代码

AC 记录。

C++
#include<bits/stdc++.h>
using namespace std;
int n[7], cnt;
int main() {
	string a;
	for (int i = 0; i < 6; ++i) {
		cin >> a;  // 每次读入一个单词,共 6 次
		if (a == "one" || a == "a" || a == "another" || a == "first") {  // 步骤一 和 二
			n[cnt++] = 1;
		} else if (a == "two" || a == "both" || a == "second") {
			n[cnt++] = 4;
		} else if (a == "three" || a == "third") {
			n[cnt++] = 9;
		} else if (a == "four") {
			n[cnt++] = 16;
		} else if (a == "five") {
			n[cnt++] = 25;
		} else if (a == "six") {
			n[cnt++] = 36;
		} else if (a == "seven") {
			n[cnt++] = 49;
		} else if (a == "eight") {
			n[cnt++] = 64;
		} else if (a == "nine") {
			n[cnt++] = 81;
		} else if (a == "ten") {
			n[cnt++] = 0;
		} else if (a == "eleven") {
			n[cnt++] = 21;
		} else if (a == "twelve") {
			n[cnt++] = 44;
		} else if (a == "thirteen") {
			n[cnt++] = 69;
		} else if (a == "fourteen") {
			n[cnt++] = 96;
		} else if (a == "fifteen") {
			n[cnt++] = 25;
		} else if (a == "sixteen") {
			n[cnt++] = 56;
		} else if (a == "seventeen") {
			n[cnt++] = 89;
		} else if (a == "eighteen") {
			n[cnt++] = 24;
		} else if (a == "nineteen") {
			n[cnt++] = 61;
		} else if (a == "twenty") {
			n[cnt++] = 0;
		}
	}
	if (cnt == 0) {  // 如果没有数字,就输出 0
		cout << 0;
		return 0;
	}
	sort(n, n + cnt);  // 从小到大排序
    bool f = 1;  // f = 1 表示为开头
	for (int i = 0; i < cnt; ++i) {
		if (f) {  // 如果是开头的话,输出非 0 数,因为开头要去零
            if (n[i] != 0) {
                cout << n[i];
                f = 0;  // 开头已经输出了,设为 0
            }
        } else {  // 如果是非开头,补零至两位
            printf("%02d", n[i]);
        }
	}
    if (f) cout << 0;  // 如果还没有输出开头输出 0
	return 0;
}
原文