A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© acdd112 中级黑马   /  2015-11-20 23:22  /  1229 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

题目: 小明从2006年1月1日开始,每三天结识一个美女两天结识一个帅哥,编程实现当输入2006年1月1日之后的任意一天,输出小明那天是结识美女还是帅哥(注意润年问题)
核心思想:
1.求出输入日期在当年是第几天
2.计算出之前经历过几个闰年
3.计算出从开始日期至输入日期的总天数
4.利用取余运算判断题目要求
  1. #include <stdio.h>
  2. /* 判断该年是否是闰年 */
  3. int whichYear(int year)
  4. {
  5.     if((year%4==0 && year%100!=0) || year%400==0)
  6.         return 1;
  7.     else
  8.         return 0;
  9. }

  10. /* 计算日期为当年的第几天 */
  11. int whichDay(int day, int month, int leapYear)
  12. {
  13.     int days[2][13] =
  14.     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,         //平年每月天数
  15.      0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};     //闰年每月天数
  16.    
  17.     /* 判断日期输入是否正确 */
  18.     if (month < 1 || month > 12)
  19.     {
  20.         printf("日期输入错误!\n");
  21.         return 0;
  22.     }
  23.    
  24.     if (day > days[leapYear][month] || day < 1)
  25.     {
  26.         printf("日期输入错误!\n");
  27.         return 0;
  28.     }
  29.    
  30.     /* 总天数 = 当月日期 + 之前月天数总和 */
  31.     for (int i = 0; i < month; i++)
  32.         day += days[leapYear][i];
  33.     return day;
  34. }

  35. /* 计算出总天数,并作出判断 */
  36. int countDay(int day, int month, int year)
  37. {
  38.     int countLeap = 0;      //闰年计数
  39.     int sumDay = 0;         //总天数
  40.     int fristDay = whichDay(1,1,whichYear(2006));    //2006年1月1日为当年第几天
  41.    
  42.     if ( !(sumDay =  whichDay(day,month,whichYear(year))))  //验证输入日期是否正确
  43.         return 1;
  44.    
  45.     //计算出过了多少个闰年
  46.     for(int i = 2006; i < year; i++)
  47.     {
  48.         if (whichYear(i))
  49.             countLeap++;
  50.     }
  51.    
  52.     sumDay += 365*(year - 2006) + countLeap - fristDay + 1; //计算总天数
  53.     printf("从2006年1月1日至此共经历%d天\n",sumDay);
  54.     //每3天认识美女
  55.     if(sumDay % 3 == 0)
  56.         printf("小明认识美女\n");
  57.     //每2天认识帅哥
  58.     if(sumDay % 2 == 0)
  59.         printf("小明认识帅哥\n");
  60.     //其余天数自己玩
  61.     if(sumDay % 2 != 0 && sumDay % 3 != 0)
  62.         printf("小明自己玩\n");
  63.     return 0;
  64. }


  65. int main()
  66. {
  67.     int day = 0, month = 0, year = 0; // 日 月 年
  68.     int flag = 1;
  69.     while (flag) {
  70.         printf("请输入日期(month/day/year):\n");
  71.         scanf("%d/%d/%d", &month, &day, &year);
  72.         if (year < 2006)
  73.         {
  74.             printf("日期输入错误!\n");
  75.             continue;
  76.         }
  77.        flag = countDay(day, month, year);
  78.     }
  79.    
  80.     return 0;
  81. }
复制代码



10 个回复

倒序浏览
太牛了!!!!!厉害!!!!非常好,思路很清晰,之前一直不知道怎么实现这段代码,看的一目了然,非常感谢,果断复制带走回去研究!!!!6666666666666
回复 使用道具 举报
太牛了!!!!!厉害!!!!非常好,思路很清晰,之前一直不知道怎么实现这段代码,看的一目了然,非常感谢,果断复制带走回去研究!!!!6666666666666
回复 使用道具 举报
闰年处理的思路很清晰,学习了。
回复 使用道具 举报
给力,值得好好研究学习。
回复 使用道具 举报
写的一目了然,不错
回复 使用道具 举报
学习学习
回复 使用道具 举报
拿去研究下 谢谢分享
回复 使用道具 举报
chensc 金牌黑马 2015-11-22 18:22:42
9#
学习学习!
回复 使用道具 举报
顺便写一下学习学习!
  1. /*
  2. 题目: 小明从2006年1月1日开始,每三天结识一个美女两天结识一个帅哥,
  3. 编程实现当输入2006年1月1日之后的任意一天,输出小明那天是结识美女还是帅哥(注意润年问题)
  4. */
  5. #include <stdio.h>
  6. typedef struct Date{
  7.     int year;
  8.     int month;
  9.     int day;
  10. } Date;

  11. /**
  12. *  是否大于其实日期
  13. *
  14. *  @param date1 <#date1 description#>
  15. *  @param date2 <#date2 description#>
  16. *
  17. *  @return <#return value description#>
  18. */
  19. int campare(Date date1,Date bgDate)
  20. {
  21.     if (date1.year>=bgDate.year){
  22.         return 1;
  23.     }
  24.     return 0;
  25. }

  26. /**
  27. *  是否是闰年
  28. *
  29. *  @param date <#date description#>
  30. *
  31. *  @return <#return value description#>
  32. */
  33. int isLeapYear(int year)
  34. {
  35.     //闰年:1、能整除4且不能整除100  2、能整除400
  36.     if ((year%4==0&&year%100!=0)||year%400==0) {
  37.         return 1;
  38.     }
  39.     return 0;
  40. }

  41. /**
  42. *  计算输入日期距2006-1-1有多少天
  43. *
  44. *  @param date <#date description#>
  45. */
  46. int count(Date date)
  47. {
  48.     Date bgDate = {2006,1,1};
  49.     int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31};//非闰年一年每个月天数
  50.     int sum = 0;
  51.     if (campare(date,bgDate)) {
  52.         if (date.year - bgDate.year!=0) {
  53.             //不是同一年
  54.             //年
  55.             for (int i = 0; i<date.year-bgDate.year; i++) {
  56.                
  57.                 if (isLeapYear(bgDate.year+i)) {
  58.                     sum += 366;
  59.                 }else{
  60.                     sum += 365;
  61.                 }
  62.             }
  63.             //月
  64.             for (int i = 0; i<date.month-1; i++) {
  65.                 sum += arr[i];
  66.             }
  67.             return sum+date.day;
  68.         }else{
  69.             //同一年
  70.             for (int i = 0; i<date.month-1; i++) {
  71.                 sum += arr[i];
  72.             }
  73.             return sum+date.day;
  74.         }
  75.     }
  76.     return 0;

  77. }

  78. /**
  79. *  是否是帅哥
  80. *
  81. *  @param date <#date description#>
  82. *
  83. *  @return <#return value description#>
  84. */
  85. int isMan(Date date)
  86. {
  87.     if (count(date)%5==0||count(date)%5==4) {
  88.         return 1;
  89.     }
  90.     return 0;
  91. }

  92. int main(){
  93.     Date date;
  94.     int flag = 1;
  95.     while (flag) {
  96.         printf("请输入:\n");
  97.         scanf("%d-%d-%d",&(date.year),&(date.month),&(date.day));
  98.         if (isMan(date)) {
  99.             printf("帅哥!\n");
  100.         }else{
  101.             printf("美女!\n");
  102.         }
  103.     }
  104.    
  105.     return 0;
  106. }
复制代码
回复 使用道具 举报
MrK 初级黑马 2015-11-23 00:30:56
11#
路过帮顶。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马