黑马程序员技术交流社区
标题:
一道关于闰年的问题
[打印本页]
作者:
敢于承担
时间:
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 编辑
我也写了一个,分享下:
#include <stdio.h>
typedef struct {
int year; // 年
int mouth; // 月
int day; // 日
int isRy; // 是否是闰年:1:是 0:不是
int countRy; // 距2006年有多少个闰年
}Date;
// 计算是否是闰年
int isRY(int year)
{
if (0 == (year % 400) &&
(0 == (year % 4) || 0 != (year % 100))) {
return 1;
}
return 0;
}
// 计算距2006年有多少个闰年
int countOfYearTo2006(int year)
{
int count = 0;
for (int i = 2006; i < year; i++) {
if (isRY(i)) {
count++;
}
}
return count;
}
// 输入年份
void inputYear(Date *d)
{
int temp;
while (1) {
printf("请输入年份:");
scanf("%d", &temp);
if (temp >= 2006) {
d->year = temp;
d->isRy = isRY(temp);
d->countRy = countOfYearTo2006(temp);
return;
}
printf("年份非法!\n");
}
}
// 输入月份
void inputMouth(Date *d)
{
int temp;
while (1) {
printf("请输入月份:");
scanf("%d", &temp);
if (temp >= 1 && temp <= 12) {
d->mouth = temp;
return;
}
printf("月份非法!\n");
}
}
// 计算每个月最多有几天
int daysOfMouth(Date *d)
{
int maxDay = 0;
int mouth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
maxDay = mouth[d->mouth - 1];
if (d->isRy && d->mouth == 2) {
maxDay++;
}
return maxDay;
}
// 输入日期
void inputDay(Date *d)
{
int temp;
int maxDay;
while (1) {
printf("请输入日期:");
scanf("%d", &temp);
maxDay = daysOfMouth(d);
if (temp >= 1 && temp <= maxDay) {
d->day = temp;
return;
}
printf("日期非法!\n");
}
}
// 计算到2006年1月1日是第几天
int daysTo2006(Date *d)
{
int dayYear = (d->year - 2006) * 365 + d->countRy;
int mouth[12] = {0, 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30};
int dayMouth = 0;
for (int i = 0; i < d->mouth; i++) {
dayMouth += mouth[i]; // 计算前面mouth-1个月有几天
}
if (d->isRy && d->mouth > 2) {
dayMouth++; // 闰年且大于2月份加一天
}
int dayDay = d->day;
return (dayDay + dayMouth +dayYear);
}
// 计算当天遇到的是美女还是帅哥
void peopleWillBeMeet(Date *d)
{
int days = daysTo2006(d); // 计算到2006年1月1日是第几天
switch (days % 3) {
case 0:
printf("距离2006年1月1日第%d天,你今天将结识新的美女!\n", days);
break;
case 1:
printf("距离2006年1月1日第%d天,洗洗睡吧!你今天不可能认识美女帅哥的\n", days);
break;
case 2:
printf("距离2006年1月1日第%d天,你今天将结识新的帅哥!\n", days);
break;
default:
break;
}
}
int main()
{
Date d = {0};
while (1) {
memset(&d, 0, sizeof(Date)); // 清空Date
inputYear(&d); // 输入年份
inputMouth(&d); // 输入月份
inputDay(&d); // 输入日期
peopleWillBeMeet(&d); // 计算当天结识的是什么人
}
return 0;
}
复制代码
作者:
程浩
时间:
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