思路
-
如果是数字:就从字符串中读入,十位
names[v / 13 + 12]
,个位names[v % 13]
-
如果是一位数,就直接
names[v % 13]
-
如果是两位数,先判断是不是整十的,整十的只输出十位。
-
如果是英文:遍历一遍 names[] 找同样的词
-
小于13:就返回i
-
不然它应该是十位,返回
(i - 12) * 13
-
-
知识点
stringsteam ssin (line) 从字符串里读东西出来,每次读一整行
记得把回车干掉,用 getchar()
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
int main()
{
int n;
cin >> n;
getchar();
while(n -- )
{
string line;
getline(cin, line);
stringstream ssin(line);
int v;
ssin >> v ;
cout << v << endl;
}
return 0;
}
y总优雅代码
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
char names[][5] = {
"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou",
};
int get(string word)
{
for (int i = 0; i < 25; i ++ )
if (word == names[i])
{
if (i < 13) return i;
else return (i - 12) * 13;
}
return 0;
}
int main()
{
int n;
cin >> n;
getchar();
while(n -- )
{
string line;
getline(cin, line);
stringstream ssin(line); // 从字符串里读
if (line[0] <= '9') // 说明是个数字
{
int v;
ssin >> v;
if (v < 13) cout << names[v % 13] << endl;
else
{
if (v % 13 == 0) cout << names[v / 13 + 12] << endl;
else cout << names[v / 13 + 12] << ' ' << names[v % 13] << endl;
}
}
else
{
int res = 0;
string word;
while(ssin >> word) res += get(word);
cout << res << endl;
}
}
return 0;
}
我被自己的弱智蠢哭了。真正见识到,什么叫弱智,就是我。
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
char ge[][5] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char shi[][5] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int number(string str)
{
int res = 0;
for (int i = 0; i < str.size(); i ++ )
{
res = res * 10 + str[i] - '0';
}
return res;
}
int get(string str)
{
for (int i = 0; i < 13; i ++ )
{
if (str == shi[i]) return i;
}
return 0;
}
int main()
{
int n;
cin >> n;
getchar();
while(n -- )
{
string str;
getline (cin , str);
stringstream ssin(str);
if (str[0] <= '9')
{
int num = number(str);
if (num == 0) cout << "tret" << endl;
else if (num % 13 == 0 && num != 0) cout << shi[num / 13] << endl;
else if (num < 13) cout << ge[num % 13] << endl;
else cout << shi[num / 13] << ' ' << ge[num % 13] << endl;
}
else
{
string r1, r2;
int g, s;
if (str.size() > 4)
{
for (int i = 0; i < 3; i ++ ) r1 += str[i];
for (int i = 0; i < 13; i ++ )
if(r1 == shi[i]) s = i ;
for (int i = 4; i < 7; i ++ ) r2 += str[i];
for (int i = 0; i < 13; i ++ )
if(r2 == ge[i]) g = i;
cout << s * 13 + g << endl;
}
else
{
for (int i = 0; i < 3; i ++ ) r1 += str[i];
//cout << r1 <<endl;
for (int i = 0; i < 13; i ++ )
{
if (r1[0] == 't' && r1[1] == 'r')
{
cout << "0" << endl;
break;
}
else
{
if (r1 == shi[i]) cout << i * 13 << endl;
if (r1 == ge[i]) cout << i << endl ;
}
}
}
}
}
return 0;
}
比起弱智,看来还是我略胜一筹了:(