信息学奥赛一本通 2057:【例3.9 】星期几
作者:互联网
【题目链接】
【题目考点】
1. switch语句
2. if…else if语句
3. 字符数组或string类数组
【题解代码】
解法1:switch语句
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
switch(n)
{
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "input error!";
}
return 0;
}
解法2:if…else if语句
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
if(n == 1)
cout << "Monday";
else if(n == 2)
cout << "Tuesday";
else if(n == 3)
cout << "Wednesday";
else if(n == 4)
cout << "Thursday";
else if(n == 5)
cout << "Friday";
else if(n == 6)
cout << "Saturday";
else if(n == 7)
cout << "Sunday";
else
cout << "input error!";
return 0;
}
解法3:使用string类数组
设置一个string类数组s,使得s[i]的值为星期i的英文单词。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i;
string s[8] = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
cin >> i;
if(i >= 1 && i <= 7)
cout << s[i];
else
cout << "input error!";
return 0;
}
标签:语句,std,string,2057,int,namespace,奥赛,include,3.9 来源: https://blog.csdn.net/lq1990717/article/details/120770285