因此,程序应要求输入一天,然后将其表示为整数值(例如,
0 =星期日或1 =星期一等),然后用户输入n天数(例如10天),程序必须查找10天后的哪一天(从星期日到星期六)。 (我知道我可以很轻松地使用循环来解决它,但是我更喜欢不使用它,谢谢。)
#include <iostream>
#include <string>
int main() {
std::string today;
std::cout <<"What day is today: " << std::endl;
std::getline (std::cin, today);
int d_ay;
std::cout << "How many days to add ";
std::cin >> d_ay;
if (today == "Monday" or 1){
today = 1 or "Monday";
}
if (today == "Tuesday"){
today = 2;
}
if (today == "Wednesday"){
today = 3;
}
if (today == "Thursday"){
today = 4;
}
if (today == "Friday"){
today = 5;
}
if (today == "saturday"){
today = 6;
}
if (today == "Sunday"){
today = 0;
}
int meet;
if(d_ay > 6){
if (d_ay > 20){
meet = (today + d_ay)/6;
}
}
return 0;
}
这就是我所走的路。
最佳答案
由于"Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"
的周期每7天重复一次,因此d_ay
只需要7种情况。
但是,通过使用modulo
的概念,可以使此过程更加优雅。 Mod由C++中的%
符号表示,并且它们的定义如下:a % b = the remainder when a is divided by b.
例如,4 % 3 = 1
和4 % 2 = 0
。
现在,我们可以编写一个修改后的程序:
#include <iostream>
#include <string>
using namespace std;
int main(){
vector <string> days = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
// Input
string today;
cout << "Enter today: ";
cin >> today;
int increment;
cout << "Enter the increment: ";
cin >> increment;
cout << days[(i + increment)%7] << endl;
return 0;
}
关于c++ - 我正在尝试制作一个程序,在该程序中添加n天(整数)后告诉一个人是一天(例如,星期一等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62379000/