zoukankan      html  css  js  c++  java
  • 邏輯題 是去打漁還是回家曬網?

    題:某人从1990年1月1日起开始“三天打鱼两天晒网”,

       问这个人在以后的某一天中是“打鱼”还是“晒网”。

    思路:根据题意可以将解题过程分为三步:
       1)计算从1990年1月1日开始至指定日期共有多少天;
       2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除;
       3)根据余数判断他是在“打鱼”还是在“晒网”;
       若 余数为1,2,3,则他是在“打鱼”
       否则 是在“晒网”
       在这三步中,关键是第一步。求从1990年1月1日至指定日期有多少天,要判断经历年份中是否有闰年,二月为29天,平年为28天。闰年的方法可以用伪语句描述如下:
       如果 ((年能被4除尽 且 不能被100除尽)或 能被400除尽)
       则 该年是闰年;
       否则 不是闰年。
       C语言中判断能否整除可以使用求余运算(即求模)

    解:

      #include<stdio.h>

      int days(struct date day);
      struct date{
        int year;
        int month;
        int day;

      };

      int main()
      {
        struct date today,term;
        int yearday,year,day;
        printf("Enter year/month/day:");
        scanf("%d%d%d",&today.year,&today.month,&today.day); /*输入日期*/
        term.month=12; /*设置变量的初始值:月*/
        term.day=31; /*设置变量的初始值:日*/
        for(yearday=0,year=1990;year<today.year;year++)
        {
          term.year=year;
          yearday+=days(term); /*计算从1990年至指定年的前一年共有多少天*/
        }
        yearday+=days(today); /*加上指定年中到指定日期的天数*/
        day=yearday%5; /*求余数*/
        if(day>0&&day<4){

          printf("he was fishing at that day.\n"); /*打印结果*/

        }
        else{

          printf("He was sleeping at that day.\n");

        }
      }

      int days(struct date day){
        static int day_tab[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 i,lp;
        lp=day.year%4==0&&day.year%100!=0||day.year%400==0;
        /*判定year为闰年还是平年,lp=0为平年,非0为闰年*/
        for(i=1;i<day.month;i++){ /*计算本年中自1月1日起的天数*/  
          day.day+=day_tab[lp][i];
          return day.day;
        }

      }

     

  • 相关阅读:
    sql左外连接、右外连接、group by、distinct(区别)、intersect(交叉)、通配符、having
    nvarchar,varchar 区别
    链家笔试链家——找寻最小消费获取最大平均分java
    利用SpringAOP 实现 日志输出
    AOP 学习笔记
    Spring AOP中pointcut expression表达式解析
    基于@Aspect的AOP配置
    URI 中特殊字符处理
    给电脑设置视力保护色
    Spring不支持依赖注入static静态变量
  • 原文地址:https://www.cnblogs.com/only_J/p/1610369.html
Copyright © 2011-2022 走看看