题目描述
(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)把这些两位数按数位排成一行,组成一个新数,如果开头为 00,就去 00。
(4)找出所有排列方法中最小的一个数,即为密码。
输入格式
一个含有 6 个单词的句子。
输出格式
一个整型变量(密码)。如果没有符合要求的数字出现,则输出 0。
分析:
对输入的英文进行判定,如果满足上面的,将数字平方后对100取模的结果放到数组中,并且记录(c)有多少个满足条件。数组设置初始量为99(最大),最后将数组进行排序。要注意取模之后的结果要是两位数,如果不是则用0补位,将排完序的数组,进行输出,只输出c个,因为只有c个满足。没有满足的,则输出0;
#include<bits/stdc++.h>
using namespace std;
int main() {
string s[6];
int a[6] = { 99,99,99,99,99,99 };
int c = 0;
for (int i = 0; i < 6; i++) {
cin >> s[i];
if (s[i] == "one" || s[i] == "another" || s[i] == "a" || s[i] == "frist") {
a[i] = 1;
c++;
}
else if (s[i] == "two" || s[i] == "both" || s[i] == "second") {
a[i] = 4;
c++;
}
else if (s[i] == "three" || s[i] == "third") {
a[i] = 9;
c++;
}
else if (s[i] == "four") {
a[i] = 16;
c++;
}
else if (s[i] == "five") {
a[i] = 25;
c++;
}
else if (s[i] == "six") {
a[i] = 36;
c++;
}
else if (s[i] == "seven") {
a[i] = 49;
c++;
}
else if (s[i] == "eight") {
a[i] = 64;
c++;
}
else if (s[i] == "nine") {
a[i] = 81;
c++;
}
else if (s[i] == "eleven") {
a[i] = 21;
c++;
}
else if (s[i] == "twelve") {
a[i] = 44;
c++;
}
else if (s[i] == "thirteen") {
a[i] = 69;
c++;
}
else if (s[i] == "fourteen") {
a[i] = 96;
c++;
}
else if (s[i] == "fifteen") {
a[i] = 25;
c++;
}
else if (s[i] == "sixteen") {
a[i] = 56;
c++;
}
else if (s[i] == "seventeen") {
a[i] = 89;
c++;
}
else if (s[i] == "eighteen") {
a[i] = 24;
c++;
}
else if (s[i] == "nineteen") {
a[i] = 61;
c++;
}
}
sort(a,a+6);//排序
if (a[0] == 99) {//表示没有符合要求的数字
cout << 0;
return 0;
}
for (int i = 0; i < c; i++) {
//两位数,如果是一位数则用0补位,第一个数字不需要
if (i != 0 && a[i] > 0 && a[i] <= 9) {
cout << "0" << a[i];
}
else {
cout << a[i];
}
}
return 0;
}