在c++中,如果今天是星期三,后天就是星期五;如果今天是星期六,后天就是星期一。用数字1到7对应星期一到星期日。给定某一天,请输出那天的“后天”是星期几。
时间: 2024-06-13 20:03:59 浏览: 327
题目要求根据输入的数字1到7对应星期一到星期日,输出这一天的“后天”是星期几。可以使用取模运算符%来实现,具体方法如下:
1.读入一个整数N,表示今天是星期N。
2.计算后天是星期几,即(N+2)%7,如果结果为0,则表示后天是星期日,需要特殊处理,即将结果赋值为7。
3.输出后天是星期几。
--代码示例--
```
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
int M = (N + 2) % 7;
if (M == 0) {
M = 7;
}
cout << M << endl;
return 0;
}
```
相关问题
计算鸭新学了关于星期的英文单词,比如“Monday”就是星期一,现在给定两个单词,表示同一周内的两天(可能会相等),都是星期一到星期日里面的英文单词,计算鸭想知道这两天的间隔,比如“Monday” 到 “Friday” 间隔就是星期一到星期五的间隔 4。 本题用“Mon”表示星期一,用“Tue”表示星期二,用“Wed”表示星期三,用“Thu”表示星期四,用“Fri”表示星期五,用”Sat“表示星期六,用”Son“表示星期日。c++代码实现
现在是 CSDN 开发的 ChitGPT,我明白您的问题并会给您答案。以下是计算鸭想知道两个星期几之间的间隔的 C 代码实现:
```c
#include <stdio.h>
#include <string.h>
// 将星期几字符串转换为数字
int dayNumber(char* day) {
if (strcmp(day, "Mon") == 0) return 1;
if (strcmp(day, "Tue") == 0) return 2;
if (strcmp(day, "Wed") == 0) return 3;
if (strcmp(day, "Thu") == 0) return 4;
if (strcmp(day, "Fri") == 0) return 5;
if (strcmp(day, "Sat") == 0) return 6;
if (strcmp(day, "Sun") == 0) return 7;
return 0;
}
int main() {
char day1[4], day2[4];
printf("请输入两个星期几字符串:\n");
scanf("%s %s", day1, day2);
int day1Number = dayNumber(day1);
int day2Number = dayNumber(day2);
int diff = day2Number - day1Number;
if (diff < 0) diff += 7;
printf("两个星期几之间的间隔是 %d 天\n", diff);
return 0;
}
```
希望我的回答对您有所帮助!
c++请输入星期几的第一个字母来判断是星期几,如果第一个字母一样,则判断第二个字母。(星期一:Monday,星期二:Tuesday,星期三:Wednesday,星期四:Thursday,星期五:Friday,星期六:Saturday,星期天:Sunday)
好的,以下是一个简单的 C++ 程序,可以实现您的要求:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string day;
cout << "请输入星期几的第一个字母:";
cin >> day;
if (day == "M" || day == "m") {
cout << "Monday" << endl;
} else if (day == "T" || day == "t") {
cout << "请输入第二个字母:";
cin >> day;
if (day == "u" || day == "U") {
cout << "Tuesday" << endl;
} else if (day == "h" || day == "H") {
cout << "Thursday" << endl;
} else {
cout << "输入有误!" << endl;
}
} else if (day == "W" || day == "w") {
cout << "Wednesday" << endl;
} else if (day == "F" || day == "f") {
cout << "Friday" << endl;
} else if (day == "S" || day == "s") {
cout << "请输入第二个字母:";
cin >> day;
if (day == "a" || day == "A") {
cout << "Saturday" << endl;
} else if (day == "u" || day == "U") {
cout << "Sunday" << endl;
} else {
cout << "输入有误!" << endl;
}
} else {
cout << "输入有误!" << endl;
}
return 0;
}
```
这个程序会要求用户输入星期几的第一个字母,然后根据输入的字母判断是星期几。如果第一个字母为 T 或者 S,程序会再次要求用户输入第二个字母,以判断具体是星期二、星期三、星期六还是星期天。如果输入有误,程序会输出提示信息。
阅读全文
相关推荐














