zoukankan      html  css  js  c++  java
  • 三天打鱼两天晒网

     

    /*
    中国有句俗语叫"三天打鱼两天晒网"。某人从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&&para.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;
    }

  • 相关阅读:
    Maven安装
    (字符串哈希表)找到字符串中不重复出现字符的最长子串长度
    (动态规划)给定三角形,找到从上到下的最小路径总和
    (链表)删除链表的重复元素
    (树)判断二叉树是否为BST
    (动态规划)机器人走迷宫问题
    (数组)Largest Rectangle in Histogram(栈解问题)
    (树)每一层节点的右指针问题(层次遍历)
    (字符串)count and say
    (贪心)加油站绕圈问题
  • 原文地址:https://www.cnblogs.com/fuyanan/p/3343994.html
Copyright © 2011-2022 走看看