P1603 斯诺登的密码

1.题目介绍

斯诺登的密码

题目背景

根据斯诺登事件出的一道水题

题目描述

(1)找出句子中所有用英文表示的数字 (20),列举在下:

正规:one two three four five six seven eight nine ten eleven twelve
thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty

非正规:a both another first second third。为避免造成歧义,another 算作 1 处理。

(2)将这些数字平方后对 100 取模,如 00,05,11,19,86,99

(3)把这些两位数按数位排成一行,组成一个新数,如果开头为 0,就去 0

(4)找出所有排列方法中最小的一个数,即为密码。

// 数据已经修正 By absi2011 如果还有问题请联系我

输入格式

一个含有 6 个单词的句子。

输出格式

一个整型变量(密码)。如果没有符合要求的数字出现,则输出 0

样例 #1

样例输入 #1

Black Obama is two five zero .

样例输出 #1

425

2.题解

2.1 字符串函数(打表)

思路

主要思路还是打表,然后将所有有效数据存储到一个vector中并排序,越小的数排在前面,最终组成的数也是最小的
为何呢?首先讨论一下个位数和十位数的情况:
1.放在首位,个位数其实组成是0x,放在首位直接整个组成数位数小一个,肯定小
2.放在中间,由于组成是0x,实际上跟十位数是一样的,但是0肯定小于其他数,所以还是小
如果是首个数字的话,0可以省略,其他情况0不可以省略

然后一定要记住如果一个数字都没有的话,要输出0!!!

代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
	vector<int> arr;
	unordered_map<string, int> word = {
		{"zero", 0},
		{"a", 1},
		{"another", 1},
		{"both", 2},
		{"first", 1},
		{"second", 2},
		{"third", 3},
		{"one", 1},
		{"two", 2},
		{"three", 3},
		{"four", 4},
		{"five", 5},
		{"six", 6},
		{"seven", 7},
		{"eight", 8},
		{"nine", 9},
		{"ten", 10},
		{"eleven", 11},
		{"twelve", 12},
		{"thirteen", 13},
		{"fourteen", 14},
		{"fifteen", 15},
		{"sixteen", 16},
		{"seventeen", 17},
		{"eighteen", 18},
		{"nineteen", 19},
		{"twenty", 20}
		
	};
	string str;
	for(int i = 0; i < 6; i++){
		cin >> str;
		if(word.count(str)){
			int temp = word[str] * word[str] % 100;
			if (temp != 0)  arr.push_back(temp);
		}
	}
    // 写到这的时候刚好测试数据次数没了,不知道为啥第三个数据一直过不去,结果应该是
	if(arr.empty()){
		cout << "0";
		return 0;
	}
	sort(arr.begin(), arr.end());
	for(auto it = arr.begin(); it != arr.end(); it++){
		if(it != arr.begin() && *it < 10) cout << 0;
		cout << *it;
	}
} 
posted @   DawnTraveler  阅读(76)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示