思路及代码
第一版、第二版代码 OJ 判定为抄袭,心“碎”了...
换了第三版代码
用 string 数组保存对应的英文
//input 多组 number int 0<= <=9
#include<iostream>
#include<string>
using namespace std;
int main(){
int num;
string ans[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
while (cin >> num){
//solution 对应英文
switch(num){
case 0:
cout << ans[0] << endl;
break;
case 1:
cout << ans[1] << endl;
break;
case 2:
cout << ans[2] << endl;
break;
case 3:
cout << ans[3] << endl;
break;
case 4:
cout << ans[4] << endl;
break;
case 5:
cout << ans[5] << endl;
break;
case 6:
cout << ans[6] << endl;
break;
case 7:
cout << ans[7] << endl;
break;
case 8:
cout << ans[8] << endl;
break;
case 9:
cout << ans[9] << endl;
break;
}
}
return 0;
}
第一版 switch 直接输出对应英文
#include<iostream>
using namespace std;
int main(){
int num;
while (cin >> num){
//solution output 对应英文
switch(num){
case 0:
cout << "Zero" << endl;
break;
case 1:
cout << "One" << endl;
break;
case 2:
cout << "Two" << endl;
break;
case 3:
cout << "Three" << endl;
break;
case 4:
cout << "Four" << endl;
break;
case 5:
cout << "Five" << endl;
break;
case 6:
cout << "Six" << endl;
break;
case 7:
cout << "Seven" << endl;
break;
case 8:
cout << "Eight" << endl;
break;
case 9:
cout << "Nine" << endl;
break;
}
}
return 0;
}
第二版换 if 直接输出对应英文
#include<iostream>
using namespace std;
int main(){
int n;
while (cin >> n){
//solution output 对应英文
if (n == 0){
cout << "Zero";
}else if (n == 1){
cout << "One";
}else if (n == 2){
cout << "Two";
}else if (n == 3){
cout << "Three";
}else if (n == 4){
cout << "Four";
}else if (n == 5){
cout << "Five";
}else if (n == 6){
cout << "Six";
}else if (n == 7){
cout << "Seven";
}else if (n == 8){
cout << "Eight";
}else if (n == 9){
cout << "Nine";
}
cout << endl;
}
return 0;
}
参考:1️⃣c++的字符串数组string_c++ string数组-CSDN博客
收获:1️⃣复习字符串数组的创建
菜菜,不是教程,做题和学习记录