c语言趣味编程(3)打鱼还是筛网

2023-06-20,,

一、问题描述

中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的以后的某一天中是打鱼还是晒网。

二、设计思路

(1)先输入当前日期,计算该日期与1990年1月1日相隔的天数,并用变量存起来;

(2)相隔的日期对5取余,如果余数为1,2,3,则输出“今天打鱼”,若为4,5,则输出“今天晒网”;

三、程序流程图

四、伪代码

五、代码

 1 #include <iostream>
2 using namespace std;
3 class Date
4 {
5 public:
6 int year;
7 int month;
8 int day;
9 };
10 bool runnian(Date&d)
11 {
12 if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
13 {
14 return true;
15 }
16 else
17 {
18 return false;
19 }
20 }
21 bool runnian(int x)
22 {
23 if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
24 {
25 return true;
26 }
27 else
28 {
29 return false;
30 }
31 }
32 int main()
33 {
34 Date d1;
35 int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
36 int count = 0; //用来保存相隔的天数
37 cin >> d1.year;
38 cin >> d1.month;
39 cin >> d1.day;
40 for (int i = 1990; i < d1.year; i++)
41 {
42 if (runnian(i))
43 {
44 count = count + 366;
45 }
46 else
47 {
48 count = count + 365;
49 }
50 }
51 for (int i = 0; i < d1.month; i++)
52 {
53 count = count + a[i];
54 }
55 count = count + d1.day;
56 if (count % 5 == 1 || count % 5 == 2 || count % 5 == 3)
57 {
58 cout << "今天打鱼" << endl;
59 }
60 else
61 {
62 cout << "今天晒网" << endl;
63 }
64
65 return 0;
66 }

六、总结

(1)函数重载的条件:①参数的个数不同,②参数的数据类型不同;

(2)如果一个数据有多个属性和行为,可以用类class来存储;

(3)判断闰年:四年一闰,百年不闰,四百年又闰;

c语言趣味编程(3)打鱼还是筛网的相关教程结束。

《c语言趣味编程(3)打鱼还是筛网.doc》

下载本文的Word格式文档,以方便收藏与打印。