题目
题解
比输出数值对应中文的题友好多了。
将“时”、“分”分开输出即可;“时”的输出可以看作“分”的输出的部分情况。
看代码吧,解释比较详细。
注意分输出时存在为30,40,50的特殊情况,可以通过自己出样例测试出来。
代码
#include<bits/stdc++.h>
using namespace std;
string to10[11] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
// to10[0~10] = 0~10
string to20[11] = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
// to20[0~10] = 0 : "", 1~10 : 10+(1~10)
string to60[6] = {"", "", "twenty", "thirty", "forty", "fifty"};
// to60[0~5] = 0、1 : "", 2~5 : 10*(2~5)
int h, m;
int main()
{
cin>>h>>m;
if(h <= 10) cout<<to10[h]; // 不超过10
else if(h <= 20) cout<<to20[h-10]; // 不超过20
else cout<<to20[10]<<' '<<to10[h%20]; // 超过20,要进行十位数和个位数分开输出
cout<<' ';
if(m) { // 分钟非零
if(m <= 10) cout<<to10[m]; // 不超过10
else if(m <= 20) cout<<to20[m-10]; // 不超过20
else cout<<to60[m/10]<<' '<<(m%10?to10[m%10]:""); // 超过20,要进行十位数和个位数分开输出,要注意m=30,40,50时避免输出个位0,要进行特判
} else cout<<"o'clock"<<endl;
return 0;
}