三天打鱼两天晒网 C++ / Java
2016-03-23 09:51
295 查看
/*
中国有句俗语叫"三天打鱼两天晒网"。某人从1990年1月1日起开始
“三天打鱼两天晒网”,问这个人在以后的某一天中是"打鱼"还是"晒网"。
问题分析与算法设计:
根据题意可以将解题过程分为三步:
1)计算从1990年1月1日开始至指定日期共有多少天;
2)由于"打鱼"和"晒网"的周期为5天,所以将计算出的天数用5去除;
3)根据余数判断他是在"打鱼"还是在"晒网";若余数为0,1,2,则他是在"打鱼";否则是在"晒网"。
在这三步中,关键是第一步。求从1990年1月1日至指定日期有多少天,要判断经历年份中是否有闰年,
二月为29天,平年为28天。
闰年的方法可以用伪语句描述如下:如果 ((年能被4除尽且不能被100除尽)或能被400除尽)
则该年是闰年;否则不是闰年。
*/
#include <iostream>
using namespace std;
typedef struct DATE
{
int year;
int month;
int day;
}date;
int Days(date para);
int main()
{
date appointDate;
int totalDays=0; //1990.1.1到指定日期的总天数,初始化为0
cout<<"Please input your appointed day:";
cin>>appointDate.year>>appointDate.month>>appointDate.day; //输入指定日期
date temp; //指定日期前一年的最后一天
temp.month=12;
temp.day=31;
for(int year=1990;year<=appointDate.year-1;year++) //计算出1990.1.1到指定日期前一年最后一天的天数
{
temp.year=year;
totalDays+=Days(temp);
}
totalDays+=Days(appointDate); //指定年一月一日到指定日期的天数
if(totalDays%5==4||totalDays%5==0) //判断该人是“钓鱼”还是“休息”
cout<<"The person is sleeping!"<<endl;
else
cout<<"The person is fishing!"<<endl;
return 0;
}
//某年某月某日到该年1月1日的总天数
int Days(date para)
{
int i,days=0;
int days_table[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}, //平年每月平均天数
{0,31,29,31,30,31,30,31,31,30,31,30,31}}; //闰年每月平均天数
int flag=((para.year%4==0&¶.year%100!=0)||para.year%400==0); //flag==0则为平年,否则闰年
for(i=0;i<para.month;i++)
days+=days_table[flag][i];
for(i=1;i<=para.day;i++)
days++;
return days;
}
中国有句俗语叫"三天打鱼两天晒网"。某人从1990年1月1日起开始
“三天打鱼两天晒网”,问这个人在以后的某一天中是"打鱼"还是"晒网"。
问题分析与算法设计:
根据题意可以将解题过程分为三步:
1)计算从1990年1月1日开始至指定日期共有多少天;
2)由于"打鱼"和"晒网"的周期为5天,所以将计算出的天数用5去除;
3)根据余数判断他是在"打鱼"还是在"晒网";若余数为0,1,2,则他是在"打鱼";否则是在"晒网"。
在这三步中,关键是第一步。求从1990年1月1日至指定日期有多少天,要判断经历年份中是否有闰年,
二月为29天,平年为28天。
闰年的方法可以用伪语句描述如下:如果 ((年能被4除尽且不能被100除尽)或能被400除尽)
则该年是闰年;否则不是闰年。
*/
#include <iostream>
using namespace std;
typedef struct DATE
{
int year;
int month;
int day;
}date;
int Days(date para);
int main()
{
date appointDate;
int totalDays=0; //1990.1.1到指定日期的总天数,初始化为0
cout<<"Please input your appointed day:";
cin>>appointDate.year>>appointDate.month>>appointDate.day; //输入指定日期
date temp; //指定日期前一年的最后一天
temp.month=12;
temp.day=31;
for(int year=1990;year<=appointDate.year-1;year++) //计算出1990.1.1到指定日期前一年最后一天的天数
{
temp.year=year;
totalDays+=Days(temp);
}
totalDays+=Days(appointDate); //指定年一月一日到指定日期的天数
if(totalDays%5==4||totalDays%5==0) //判断该人是“钓鱼”还是“休息”
cout<<"The person is sleeping!"<<endl;
else
cout<<"The person is fishing!"<<endl;
return 0;
}
//某年某月某日到该年1月1日的总天数
int Days(date para)
{
int i,days=0;
int days_table[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}, //平年每月平均天数
{0,31,29,31,30,31,30,31,31,30,31,30,31}}; //闰年每月平均天数
int flag=((para.year%4==0&¶.year%100!=0)||para.year%400==0); //flag==0则为平年,否则闰年
for(i=0;i<para.month;i++)
days+=days_table[flag][i];
for(i=1;i<=para.day;i++)
days++;
return days;
}
相关文章推荐
- c++ 重写string类 MyString
- 与LSGO一起学“第1章 初识C++(1.6Visual Studio 2010编译器)”
- 与LSGO一起学“第1章 初识C++(1.5 C++与其他语言的区别)”
- 与LSGO一起学“第1章 初识C++(1.4学习C++之前需要先学C吗?)”
- 与LSGO一起学“第1章 初识C++(1.3 C++与C有什么不同)”
- 与LSGO一起学“第1章 初识C++(1.2 C++的发展过程)”
- 与LSGO一起学“第1章 初识C++(1.1 C++简介)”
- C++的一些小记录
- Ubuntu12.04下gdb调试C语言简单程序详解
- [leetcode] p338 Count Bits
- 第七届蓝桥杯c/c++ C组部分题解
- C++拷贝构造函数
- C++ return语句
- 【c++】字符串循环左移
- 【c++】字符串循环左移
- 初级C++3继承、多态、异常、命名空间
- 对线性表理解以及C语言实现链表的插入删除等操作。
- C++ 重载(Overload),覆盖(Override):,隐藏:
- C++高级进阶 第四季:const详解(二) 常量折叠
- Effective C++: 尽量不要在构造函数中调用虚函数(virtual-function).