解题思路:

条件明确,数字和字母表示一一对应。所以想到运用开关语句。


注意事项:

1.函数封装。为了减少操作,利用递归思想对20到60间的非特殊数字采用递归。

参考代码:

#include<iostream>

#include<string>

using namespace std;

string s(int x) {

string y;

switch (x) {

case 0: y = "zero";

break;

case 1:y = "one";

break;

case 2:y = "two";

break;

case 3:y = "three";

break;

case 4:y = "four";

break;

case 5:y = "five";

break;

case 6:y = "six";

break;

case 7:y = "seven";

break;

case 8:y = "eight";

break;

case 9:y = "nine";

break;

case 10:y = "ten";

break;

case 11:y = "eleven";

break;

case 12:y = "twelve";

break;

case 13:y = "thirteen";

break;

case 14:y = "fourteen";

break;

case 15:y = "fifteen";

break;

case 16:y = "sixteen";

break;

case 17:y = "seventeen";

break;

case 18:y = "eighteen";

break;

case 19:y = "nineteen";

break;

case 20:y = "twenty";

break;

case 30:y = "thirty";

break;

case 40:y = "forty";

break;

case 50:y = "fifty";

break;

default:

y = s(x - x % 10) +" "+ s(x % 10);  //递归一次,代码复用

break;

}

return y;

}

int main(){

int h, m;

cin >> h >> m;

string x, y;

if (m != 0) {

x = s(h);

y = s(m);

cout << x << " " << y << endl;

}

else {//m==0

x = s(h);

cout << x << " o'clock" << endl;

}

return 0;

}


点赞(0)
 

0.0分

0 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论