C++程序:将给定范围内的所有数字转换为文字
假设我们有两个数字a和b。我们将把每个数字转换成文字并逐个打印它们。将数字转换成文字意味着对于数字5,它应该打印“Five”。
因此,如果输入为a = 2,b = 6,则输出将为
Two Three Four Five Six
为了解决这个问题,我们将遵循以下步骤:
- 如果d < 0且d > 9,则
- 返回 ("超出0-9范围")
- 否则,当d等于0时,则
- 返回 ("Zero")
- 否则,当d等于1时,则
- 返回 ("One")
- 否则,当d等于2时,则
- 返回 ("Two")
- 否则,当d等于3时,则
- 返回 ("Three")
- 否则,当d等于4时,则
- 返回 ("Four")
- 否则,当d等于5时,则
- 返回 ("Five")
- 否则,当d等于6时,则
- 返回 ("Six")
- 否则,当d等于7时,则
- 返回 ("Seven")
- 否则,当d等于8时,则
- 返回 ("Eight")
- 否则,当d等于9时,则
- 返回 ("Nine")
- 从主方法中,执行以下操作:
- 对于a到b范围内的i,执行:
- solve(i)
- 换行
示例
让我们看看下面的实现,以便更好地理解:
#include <iostream>
using namespace std;
void solve(int d){
if(d < 0 || d > 9){
cout << "Beyond range of 0 - 9";
}else if(d == 0){
cout << "Zero";
}else if(d == 1){
cout << "One";
}else if(d == 2){
cout << "Two";
}else if(d == 3){
cout << "Three";
}else if(d == 4){
cout << "Four";
}else if(d == 5){
cout << "Five";
}else if(d == 6){
cout << "Six";
}else if(d == 7){
cout << "Seven";
}else if(d == 8){
cout << "Eight";
}else if(d == 9){
cout << "Nine";
}
}
int main(){
int a = 2, b = 6;
for(int i = a; i <= b; i++){
solve(i);
cout << endl;
}
}
输入
2, 6
输出
Two Three Four Five Six
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP