Cho hai chữ số a, b. Chuyển từng chữ số trong khoảng [a, b] thành từ và in ra. Ví dụ: 5 → "Five".
Ví dụ: a=2, b=6 → "Two Three Four Five Six"
Giải thuật
- Hàm solve(d):
- Nếu d<0 hoặc d>9: "Beyond range of 0 - 9"
- Switch-case 0-9: "Zero" đến "Nine"
- Main: vòng lặp i=a đến b, gọi solve(i), xuống dòng
Ví dụ minh họa
#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;
}
}
Đầu vào
2, 6
Đầu ra
Two Three Four Five Six