解题思路:本题为C++循环结构的课后习题,这里我提供了一种用循环实现的方法。
注意事项:
参考代码:
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
int mouth=6; //定义月份,我们实际上只过了6个月,第七个月还没结束,因此设为6
int date=19; //定义日期,我们在第七月里过了19天
int day; //定义答案
for(int i=1;i<=mouth;mouth--) //使用for语句,循环运算天数
{
if(mouth%2==0) //通过奇偶性,判断是大月还是小月
{
if(mouth==2) //如果是2月则天数为28天
{
day+=28;
}
else
{
day+=30; //小月天数为30天
}
}
else
{
day+=31; //大月天数为31天
}
}
cout<<day+=date; //加上日期,得出答案
return 0;
}
0.0分
1 人评分