黑马程序员技术交流社区

标题: 一道关于闰年的问题 [打印本页]

作者: 敢于承担    时间: 2014-3-23 15:34
标题: 一道关于闰年的问题
题目:小明从2006年1月1日开始,每三天结识一个美女两天结识一个帅哥,编程实现当输入2006年1月1日之后的任意一天,输出小明那天是结识美女还是帅哥(注意润年问题)
用C语言编写
作者: 1722413885    时间: 2014-3-23 17:40
#include <stdio.h>
#include <time.h>
#include <memory.h>

int main(void)
{
time_t t1;
time_t t2;
int y,m,d;
struct tm OldTime;
struct tm NewTime;
printf("输入新时间:");
scanf("%d%d%d",&y,&m,&d);//输入年月日
printf("\n%d %d %d\n",y,m,d);
memset(&OldTime,0x0,sizeof(tm));
OldTime.tm_year =2006-1900;
OldTime.tm_mon =1;
OldTime.tm_mday =1;
memset(&NewTime,0x0,sizeof(tm));
NewTime.tm_year = y-1900;// 2011 年表示为 2011 - 1900 = 111
NewTime.tm_mon = m;
NewTime.tm_mday = d;
t1 = mktime(&NewTime);
t2 = mktime(&OldTime);
printf("间隔天数=%d",(t1-t2)/(24*3600));
return 0;
}
希望能帮到你!
作者: 周宇华    时间: 2014-3-23 20:03
本帖最后由 周宇华 于 2014-3-23 22:53 编辑

我也写了一个,分享下:
  1. #include <stdio.h>

  2. typedef struct {
  3.     int year; // 年
  4.     int mouth; // 月
  5.     int day; // 日
  6.     int isRy; // 是否是闰年:1:是 0:不是
  7.     int countRy; // 距2006年有多少个闰年
  8. }Date;

  9. // 计算是否是闰年
  10. int isRY(int year)
  11. {
  12.     if (0 == (year % 400) &&
  13.         (0 == (year % 4) || 0 != (year % 100))) {
  14.         return 1;
  15.     }
  16.    
  17.     return 0;
  18. }

  19. // 计算距2006年有多少个闰年
  20. int countOfYearTo2006(int year)
  21. {
  22.     int count = 0;
  23.     for (int i = 2006; i < year; i++) {
  24.         if (isRY(i)) {
  25.             count++;
  26.         }
  27.     }
  28.     return count;
  29. }

  30. // 输入年份
  31. void inputYear(Date *d)
  32. {
  33.     int temp;
  34.     while (1) {
  35.         printf("请输入年份:");
  36.         scanf("%d", &temp);
  37.         if (temp >= 2006) {
  38.             d->year = temp;
  39.             d->isRy = isRY(temp);
  40.             d->countRy = countOfYearTo2006(temp);
  41.             return;
  42.         }
  43.         printf("年份非法!\n");
  44.     }
  45. }

  46. // 输入月份
  47. void inputMouth(Date *d)
  48. {
  49.     int temp;
  50.     while (1) {
  51.         printf("请输入月份:");
  52.         scanf("%d", &temp);
  53.         if (temp >= 1 && temp <= 12) {
  54.             d->mouth = temp;
  55.             return;
  56.         }
  57.         printf("月份非法!\n");
  58.     }
  59. }

  60. // 计算每个月最多有几天
  61. int daysOfMouth(Date *d)
  62. {
  63.     int maxDay = 0;
  64.     int mouth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  65.     maxDay = mouth[d->mouth - 1];
  66.     if (d->isRy && d->mouth == 2) {
  67.         maxDay++;
  68.     }
  69.    
  70.     return maxDay;
  71. }

  72. // 输入日期
  73. void inputDay(Date *d)
  74. {
  75.     int temp;
  76.     int maxDay;
  77.     while (1) {
  78.         printf("请输入日期:");
  79.         scanf("%d", &temp);
  80.         maxDay = daysOfMouth(d);
  81.         if (temp >= 1 && temp <= maxDay) {
  82.             d->day = temp;
  83.             return;
  84.         }
  85.         printf("日期非法!\n");
  86.     }
  87. }

  88. // 计算到2006年1月1日是第几天
  89. int daysTo2006(Date *d)
  90. {
  91.     int dayYear = (d->year - 2006) * 365 + d->countRy;
  92.    
  93.     int mouth[12] = {0, 31, 28, 31, 30, 31,
  94.                     30, 31, 31, 30, 31, 30};
  95.     int dayMouth = 0;
  96.     for (int i = 0; i < d->mouth; i++) {
  97.         dayMouth += mouth[i]; // 计算前面mouth-1个月有几天
  98.     }
  99.     if (d->isRy && d->mouth > 2) {
  100.         dayMouth++; // 闰年且大于2月份加一天
  101.     }
  102.    
  103.     int dayDay = d->day;
  104.    
  105.     return (dayDay + dayMouth +dayYear);
  106. }

  107. // 计算当天遇到的是美女还是帅哥
  108. void peopleWillBeMeet(Date *d)
  109. {
  110.     int days = daysTo2006(d); // 计算到2006年1月1日是第几天

  111.     switch (days % 3) {
  112.         case 0:
  113.             printf("距离2006年1月1日第%d天,你今天将结识新的美女!\n", days);
  114.             break;
  115.             
  116.         case 1:
  117.             printf("距离2006年1月1日第%d天,洗洗睡吧!你今天不可能认识美女帅哥的\n", days);
  118.             break;
  119.             
  120.         case 2:
  121.             printf("距离2006年1月1日第%d天,你今天将结识新的帅哥!\n", days);
  122.             break;
  123.             
  124.         default:
  125.             break;
  126.     }
  127. }

  128. int main()
  129. {
  130.     Date d = {0};
  131.    
  132.     while (1) {
  133.         memset(&d, 0, sizeof(Date)); // 清空Date
  134.         inputYear(&d); // 输入年份
  135.         inputMouth(&d); // 输入月份
  136.         inputDay(&d); // 输入日期
  137.         peopleWillBeMeet(&d); // 计算当天结识的是什么人
  138.     }
  139.     return 0;
  140. }
复制代码

作者: 程浩    时间: 2014-3-23 22:08

#ifndef ______count_h
#define ______count_h
#include <stdio.h>
#define leap(y) (y%4==0 && y%100!=0 || y%400==0)
struct d//声明一个含有3个整型的结构体。
{int y,m,d;};
long days(struct d d1,struct d d2)//函数参数是结构体类型
{
    int mon[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;
    long td=0;
    //若输入   2007-2-3
    for(i=d1.y;i<d2.y;i++)
        td+=leap(i)?366:365;//返回0:不为润年 返回1:为闰年,若d1.y为2007,则td=365
    for(i=1;i<d1.m;i++)
        td-=mon[leap(d1.y)][i];//返回0,d1.m为1,则td=365
    td-=d1.d-1;//td=333
    for(i=1;i<d2.m;i++)
        td+=mon[leap(d2.y)][i];//返回0,d2.m为2,则td+31=396
    td+=d2.d-1;//d2.d=3,td+2=368
    return td;
}
#endif

#include <stdio.h>
#include "count.h"

int main(){
    struct d d1,d2;//声明2个结构体类型变量。
    d1.y=2006;
    d1.m=1;
    d1.d=1;
    long td;
    char like[]="";
    printf("second date:");
    scanf("%d-%d-%d",&d2.y,&d2.m,&d2.d);
    td=days(d1,d2);//调用count.h中的函数
    printf("相隔:%ld\n",td);//输出相隔多少天
    if(td%5>2){
        strcat(like,"handsomeboy");//通过连接函数把字符串连接到like中
    }else {strcat(like,"nicegirl");}
    printf("小明这天认识了%s\n",like);
   
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2