所需的任務是計算週一露營開始時可以駐紮的連續天數。 露營可以在晴天(1)或多雲(2)天進行,但不能在下雨天(3天)進行。另外,它不應該太冷或太熱。的3個條件,每天以適合於野營是:將布爾函數傳遞給數組
溫度爲至少24
溫度爲至多30
天氣或者是晴天或陰天。
這是我的代碼(由於要求,主功能不能被修改並且兩個其他功能必須存在)
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天。是否有可能數不?這樣的數組中的trues?
對於第一個提示,OP使用的數組與* fixed *提供的代碼相反。 – Jarod42
@ Jarod42:謝謝你指出 - 這是一個非常糟糕的家庭作業! –