Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1292387
  • 博文数量: 168
  • 博客积分: 2124
  • 博客等级: 大尉
  • 技术积分: 2590
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-16 23:51
文章分类

全部博文(168)

文章存档

2014年(6)

2013年(74)

2012年(71)

2011年(17)

分类: LINUX

2013-08-08 01:22:29

三篇博客竟然被管理员删了,还好都有备份


点击(此处)折叠或打开

  1. /*************************************************
  2. 3. 打鱼还是晒网
  3.  
  4. * 问题描述:
  5.  
  6. 中国有句俗话叫“三天打鱼两天晒网” ,某人从 1990年1月 1 日起开始“三天打鱼两天晒网”,
  7. 问这个人在以后的某一天是“打鱼”还是“晒网”?
  8.  
  9. (需要考虑闰年:年数“能被4除尽且不能被100除尽”或“直接能被 400 除尽”看做闰年,
  10. 如 2000 年是闰年,而 1900 年不是)
  11.  
  12. * 要求实现函数:
  13.  
  14. int isWorking(unsigned int year, unsigned int month, unsigned int day);
  15. 输入:year,month,day 分别为年,,
  16. 返回:int类型,如果是“打鱼”,就返回 1;如果是“晒网”,就返回 0
  17.  
  18.  * 示例
  19.  输入:year=1990,month=1,day=5;
  20.  函数返回:0
  21. ***************************************************/

  22. #include<stdio.h>

  23. /***********************************************************
  24. 编程思路:
  25.     首先见year判断出来,之前有多少个闰年。
  26.         闰年的算法,大于1992的,用(year - 1988)/4,每次增一
  27.         小于1992,大于等于1990的,无
  28.     再算“某天”到1990.1.1有多少天。
  29.         当年是闰年,注意把这个闰年去掉,因为在当年的日子里已经加过了
  30.         当年不是闰年,num = (total * 365 + count ) + month_total + day
  31.     除5取余判断
  32. ************************************************************/

  33. int isWorking(unsigned int year, unsigned int month, unsigned int day)
  34. {
  35.     int i, count, last, total, num, month_total, flag;
  36.     
  37.     int nor[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  38.     int run[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
  39.     count = last = month_total = 0;
  40.     //count是闰年的个数,last是比上一个闰年还有几年(1992年开始).total是从1990开始的年数,num是从“某天开始”到1990.1.1有多少天
  41.     
  42.     i = year -1988;
  43.     flag = i % 4;//flag为0表示瑞年
  44.     if(year < 1990)
  45.         exit(1);
  46.         
  47.     if(year >= 1992)    
  48.             count = i / 4;
  49.     
  50.     if(year >= 1990 && year < 1992)
  51.     ;
  52.     
  53.     total = year - 1990;
  54.     printf("total = %d\n", total);    
  55.     if(!flag) //如果是闰年
  56.     {
  57.         for(i = 1; i < month; i++)
  58.             month_total += run[i];
  59.             
  60.         num = (total * 365 + count - 1) + month_total + day;//把这个闰年给减掉
  61.         
  62.     }
  63.     else //不是闰年
  64.     {
  65.         for(i = 1; i < month; i++)
  66.             month_total += run[i];
  67.             
  68.         num = (total * 365 + count ) + month_total + day;//
  69.     }

  70.     printf("num = %d\n",num);
  71.     
  72.     i = num % 5;

  73.     printf(" i = %d \n" ,i);
  74.     return (i < 4) ? 1 : 0;
  75. }
  76.     

  77. int main(int argc, char **argv)
  78. {
  79.     int result;
  80.     
  81. //    result = isWorking(1990,1,1);
  82. //    result = isWorking(1990,1,4);
  83. //    result = isWorking(1991,1,1);
  84. //    result = isWorking(1992,3,1);
  85.     result = isWorking(1997,3,1);
  86.     printf("result = %d", result);
  87.     
  88.     while(1);

  89. }

阅读(1042) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~