跳到正文

题解

题解:P1603 斯诺登的密码

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

文章信息

发布
2025年7月26日
收录
2025年12月17日
更新
2026年11月14日
篇幅
约 3 分钟
题目
P1603

版本

  1. v1当前

This is my first solution…


我的解法和别人的不同之处在于:

  1. 将基数词放在一起,序数词放在一起,a 和 both 放在一起,another 单独一组,这样可以直接用下标作为转换后的数字。
  2. 用 to_string(i*i%100); 来进行操作,可有效地节省空间。

Code:

C++
#include<bits/stdc++.h>
using namespace std;
bool cmp(const string &a,const string &b){
	return (a+b<b+a);
}
string s,k,p;
string j[21]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
string x[21]={"0th","first","second","third"};
string f[21]={"0","a","both"};
string v="another";
int a,q;
string g[7];
int main(){
	getline(cin,s);
	for(int ii=0;ii<s.size();ii++){
        if(s[ii]>='A'&&s[ii]<='Z'){
            s[ii]+=32;//注意大小写
        }
		if(s[ii]==' '){
			for(int i=0;i<=20;i++){
				if(k==j[i]||k==x[i]||k==f[i]){
                    q++;
					g[++a]=to_string(i*i%100);
					if(g[a].size()==1){
						g[a].insert(0,"0");
					}
				}
			}
			if(k==v){
                q++;
				g[++a]="01";
			}
			k="";
		}else{
			k+=s[ii];
		}
	}
	sort(g+1,g+a+1,cmp);
	for(int i=1;i<=a;i++){
		p+=g[i];
	}
    while(p[0]=='0'){
        p.erase(0,1);
    }
    if(q==0){//如果没有合法的数字
        cout<<q;
    }else{
        cout<<p;
    }
    return 0;
}
原文
存档评论1 条抓取于