友情提示:380元/半年,儿童学编程,就上码丁实验室。
如果一个渔夫从2011年1月1日开始每三天打一次渔,两天晒一次网,编程实现当输入2011年1月1日后的任意一天,输出该渔夫是在打渔还是晒网。
思路:
1、判断输入的年份是否为闰年。参见之前的案例“计算某日是该年的第几天”。
2、求出输入的日期距离2011年1月1日有多少天。首先判断2011年距离输入的年份有多少年,这其中有几个闰年,几个平年;闰年加366天,平年加365天。其次计算输入的月日距离输入年份的1月1日有多少天。参见之前的案例“计算某日是该年的第几天”。
C语言:
#include <stdio.h>
int leap(int a) /*自定义函数leap用来指定年份是否为闰年*/
{
if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0)/*闰年判定条件*/
return 1;/*不是闰年返回1*/
else
return 0;/*不是闰年返回0*/
}
int number(int year, int m, int d)/*自定义函数number计算输入日期距2011年1月1日共有多少天*/
{
int sum = 0, i, j, k, a[12] =
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};/*数组a存放平年每月的天数*/
int b[12] =
{
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}; /*数组b存放闰年每月的天数*/
if (leap(year) == 1)/*判断是否为闰年*/
for (i = 0; i < m – 1; i++)
sum += b[i];/*是闰年,累加数组b前m-1个月份天数*/
else
for (i = 0; i < m – 1; i++)
sum += a[i];/*不是闰年,累加数组a钱m-1个月份天数*/
for (j = 2011; j < year; j++)
if (leap(j) == 1)
sum += 366;/*2011年到输入的年份是闰年的加366*/
else
sum += 365;/*2011年到输入的年份不是闰年的加365*/
sum += d; /*将前面累加的结果加上日期,求出总天数*/
return sum; /*将计算的天数返回*/
}
void main()
{
int year, month, day, n;
printf(“请输入年月日n”);
scanf(“%d%d%d”, &year, &month, &day); /*输入年月日*/
n = number(year, month, day); /*调用函数number*/
if ((n % 5) < 4 && (n % 5) > 0)/*当余数是1或2或3时说明在打渔否则在晒网*/
printf(“%d:%d:%d 打渔n”, year, month, day);
else
printf(“%d:%d:%d 晒网n”, year, month, day);
}
C++:
#include<iostream>
using namespace std;
int leap(int a) /*自定义函数leap用来指定年份是否为闰年*/
{
if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0)/*闰年判定条件*/
return 1;/*不是闰年返回1*/
else
return 0;/*不是闰年返回0*/
}
int number(int year, int m, int d)/*自定义函数number计算输入日期距2011年1月1日共有多少天*/
{
int sum = 0, i, j, k, a[12] =
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};/*数组a存放平年每月的天数*/
int b[12] =
{
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}; /*数组b存放闰年每月的天数*/
if (leap(year) == 1)/*判断是否为闰年*/
for (i = 0; i < m – 1; i++)
sum += b[i];/*是闰年,累加数组b前m-1个月份天数*/
else
for (i = 0; i < m – 1; i++)
sum += a[i];/*不是闰年,累加数组a钱m-1个月份天数*/
for (j = 2011; j < year; j++)
if (leap(j) == 1)
sum += 366;/*2011年到输入的年份是闰年的加366*/
else
sum += 365;/*2011年到输入的年份不是闰年的加365*/
sum += d; /*将前面累加的结果加上日期,求出总天数*/
return sum; /*将计算的天数返回*/
}
int main()
{
int year, month, day, n;
cout << “请输入年月日” << endl;
cin >> year >> month >> day; /*输入年月日*/
n = number(year, month, day); /*调用函数number*/
if ((n % 5) < 4 && (n % 5) > 0)/*当余数是1或2或3时说明在打渔否则在晒网*/
cout << “今天打渔” << endl ;
else
cout << “今天晒网” << endl;
return 0;
}
转自公众号:
南昌青少年编程