版权声明:个人学习笔记记录 https://blog.csdn.net/Ratina/article/details/84107239
题目:P1603
题意:
(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
(2)将这些数字平方后%100,如00,05,11,19,86,99。
(3)把这些两位数按数位排成一行,组成一个新数,如果开头为0,就去0。
(4)找出所有排列方法中最小的一个数,即为密码。
Ps.输入是一个含有6个单词的句子。
分析:
水题,字符串处理+模拟,不过要注意审题就是了。
想到了三种方法吧…
①用C的char数组和strcmp(s1,s2)函数,s1和s2相同返回0,不相同返回非零值。
②用C++的string类型和s1==s2,s1和s2相同为true,不相同为false。
③用STL的map映射,从string映射到int,这样m[“one”]=1,用数组下标的形式来直接访问会快些,也方便些。
使用 map.count ( key )函数,返回map中key元素的个数,在map里面非1即0,存在为1,不存在为0。
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
map<string,int> num;
num["zero"]=0;
num["one"]=1;
num["a"]=1;
num["another"]=1;
num["first"]=1;
num["two"]=2;
num["both"]=2;
num["second"]=2;
num["three"]=3;
num["third"]=3;
num["four"]=4;
num["five"]=5;
num["six"]=6;
num["seven"]=7;
num["eight"]=8;
num["nine"]=9;
num["ten"]=10;
num["eleven"]=11;
num["twelve"]=12;
num["thirteen"]=13;
num["fourteen"]=14;
num["fifteen"]=15;
num["sixteen"]=16;
num["seventeen"]=17;
num["eighteen"]=18;
num["nineteen"]=19;
num["twenty"]=20;
int i,cnt,ans[6];
string temp;
for(i=1,cnt=0;i<=6;i++)
{
cin>>temp;
if(num.count(temp)) //如果存在temp的映射
ans[cnt++]=(num[temp]*num[temp])%100;
}
sort(ans,ans+cnt); //从小到大排序
if(!cnt) //如果输入中没有数字
cout<<0<<endl;
else
{
for(i=0;i<cnt;i++) //找到并输出第一个不为0的数字
{
if(ans[i]!=0)
{
cout<<ans[i];
break;
}
}
if(i==cnt) //如果都为0
cout<<0<<endl;
else
{
for(i=i+1;i<cnt;i++) //输出剩余的数字,只有个位则前面补个0
{
if(ans[i]<10)
cout<<0<<ans[i];
else
cout<<ans[i];
}
cout<<endl;
}
}
return 0;
}