所需的任务是计算周一露营开始时可以营的连续天数。 露营可以在阳光明媚的(1)或阴天(2)天进行,但不能在下雨(3)天进行。此外,它不应该太冷或太热。一天适合露营的3个条件是:
温度至少为24。
温度最高为30。
天气晴朗或多云。
这是我的代码(由于要求, main 函数无法修改,其他两个函数应该存在)
bool isSuitableForCamping(int minTemp, int maxTemp, int weather)
{
if (minTemp >= 24 && maxTemp <= 30 && (weather == 1 || weather == 2))
{return true;}
else
{return false;}
}
int getMaxCampingDaysIfStartOnMonday(
int mondayMinTemp, int mondayMaxTemp, int mondayWeather,
int tuesdayMinTemp, int tuesdayMaxTemp, int tuesdayWeather,
int wednesdayMinTemp, int wednesdayMaxTemp, int wednesdayWeather,
int thursdayMinTemp, int thursdayMaxTemp, int thursdayWeather,
int fridayMinTemp, int fridayMaxTemp, int fridayWeather,
int saturdayMinTemp, int saturdayMaxTemp, int saturdayWeather,
int sundayMinTemp, int sundayMaxTemp, int sundayWeather
)
{
int dayCount = 0;
bool dayList[7];
dayList[0] = isSuitableForCamping(mondayMinTemp, mondayMaxTemp, mondayWeather);
dayList[2] = isSuitableForCamping(tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather);
dayList[3] = isSuitableForCamping(wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather);
dayList[4] = isSuitableForCamping(thursdayMinTemp, thursdayMaxTemp, thursdayWeather);
dayList[5] = isSuitableForCamping(fridayMinTemp, fridayMaxTemp,fridayWeather);
dayList[6] = isSuitableForCamping(saturdayMinTemp, saturdayMaxTemp, saturdayWeather);
dayList[7] = isSuitableForCamping(sundayMinTemp, sundayMaxTemp, sundayWeather);
for (int i = 0; i < 7; i++)
{
if (dayList[i] == true )
{
dayCount++;
}
else
break;
}
return dayCount;
}
int main()
{
//Get Monday's weather forecast data
int mondayMinTemp, mondayMaxTemp, mondayWeather;
cout << "Monday's minimum temperature: ";
cin >> mondayMinTemp;
cout << "Monday's maximum temperature: ";
cin >> mondayMaxTemp;
cout << "Monday's weather (1:Sunny 2:Cloudy 3:Rainy): ";
cin >> mondayWeather;
cout << endl;
.
.
.
//Get Sunday's weather forecast data
int sundayMinTemp, sundayMaxTemp, sundayWeather;
cout << "Sunday's minimum temperature: ";
cin >> sundayMinTemp;
cout << "Sunday's maximum temperature: ";
cin >> sundayMaxTemp;
cout << "Sunday's weather (1:Sunny 2:Cloudy 3:Rainy): ";
cin >> sundayWeather;
cout << endl;
cout << "If you start camping on Monday, you would be able to camp for "
<< getMaxCampingDaysIfStartOnMonday( mondayMinTemp, mondayMaxTemp, mondayWeather,
tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather,
wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather,
thursdayMinTemp, thursdayMaxTemp, thursdayWeather,
fridayMinTemp, fridayMaxTemp, fridayWeather,
saturdayMinTemp, saturdayMaxTemp, saturdayWeather,
sundayMinTemp, sundayMaxTemp, sundayWeather )
<< " day(s) at most!" << endl;
return 0;
}
我尝试输入值,以便前三天可用于露营。然而它总是最多让我回归1天。有可能算不上。这样的阵列中的真实?
答案 0 :(得分:1)
代码有几个问题,一个是你的数组索引关闭:
dayList[0] = isSuitableForCamping(mondayMinTemp, mondayMaxTemp, mondayWeather);
dayList[2] = isSuitableForCamping(tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather);
dayList[3] = isSuitableForCamping(wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather);
dayList[4] = isSuitableForCamping(thursdayMinTemp, thursdayMaxTemp, thursdayWeather);
dayList[5] = isSuitableForCamping(fridayMinTemp, fridayMaxTemp,fridayWeather);
dayList[6] = isSuitableForCamping(saturdayMinTemp, saturdayMaxTemp, saturdayWeather);
dayList[7] = isSuitableForCamping(sundayMinTemp, sundayMaxTemp, sundayWeather);
您跳过了元素1,然后覆盖了数组的末尾。它应该是:
dayList[0] = isSuitableForCamping(mondayMinTemp, mondayMaxTemp, mondayWeather);
dayList[1] = isSuitableForCamping(tuesdayMinTemp, tuesdayMaxTemp, tuesdayWeather);
dayList[2] = isSuitableForCamping(wednesdayMinTemp, wednesdayMaxTemp, wednesdayWeather);
dayList[3] = isSuitableForCamping(thursdayMinTemp, thursdayMaxTemp, thursdayWeather);
dayList[4] = isSuitableForCamping(fridayMinTemp, fridayMaxTemp,fridayWeather);
dayList[5] = isSuitableForCamping(saturdayMinTemp, saturdayMaxTemp, saturdayWeather);
dayList[6] = isSuitableForCamping(sundayMinTemp, sundayMaxTemp, sundayWeather);
一个建议:
答案 1 :(得分:1)
您需要更改for
周期,以便在第一个不合适的一天之后不会中断。
//this is the amount of consecutive days that we are currently counting
int currentConsecutiveDays = 0;
//this is the maximum amount of consecutive days in the whole week
int maxConsecutiveDays = 0;
for (int i = 0; i < 7; i++)
{
if (dayList[i] == true )
{
currentConsecutiveDays++;
}
//we need the second condition for the case when
//the consecutive days end up on Sunday.
else if(dayList[i] == false || i == 6)
{
if(currentConsecutiveDays > maxConsecutiveDays)
maxConsecutiveDays = currentConsecutiveDays;
currentConsecutiveDays = 0;
}
}
此外,您已跳过1
中的索引dayList
。如果数组包含N个元素,则该数组的索引应为0, 1, ..., N - 1