switch解法
2026-05-16 16:52:22
发布于:四川
9阅读
0回复
0点赞
这道题解法很多:博弈,二分,字符,字符串都不可以
咳咳
这里讲不常用的switch吧
|switch|
switch又叫开关语句,是分支结构的一种语法
语法为
switch(常量表达式){//只能是int或char
case 常量1:语句1;//常量表达式=常量,执行
case 常量2:语句2;
case 常量3:语句3;
case 常量4:语句4;break;//有break就会一直判断到这儿
······
default 语句n;//没有break就会一直判断到这儿,并结束语句
}
好
上代码
#include<iostream>
using namespace std;
int main(){
int today=1;
int week;
cin>>week;
week+=today;
while(week>7){
week-=7;
}switch(week){
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;
default:cout<<"sunday";
}
return 0;
}
这里空空如也








有帮助,赞一个