注册 登录
编程论坛 C++教室

【初学者求助】一个简单的问题

lovelosegirl 发布于 2007-07-27 06:55, 734 次点击
对各位大虾来说应该很简单吧~!
题目:输入某年某月某日,判断该日是该年的第几天.
需要各位给个提示什么的,谢谢了!
10 回复
#2
福尔摩斯2007-07-27 07:19

首先先判断年份是 闰年 还是 平年

调用2个数组 一个是平年数组 ,第二个是闰年数组

比如:平年数组a[12]={31,28,……}

输入的第几个月 ,就把n-1个月全加起来

再加上第几天,就可以了了

#3
lovelosegirl2007-07-27 07:22

谢谢~!呵呵~!

#4
lovelosegirl2007-07-27 08:28
不知道如果不用数组的话有别的更简单的方法吗?其实本人刚上C++课程还没学到数组,希望有达人可以再回复我一下!
#5
haihui2822007-07-27 10:06

#include<iostream>
#include<string>
using namespace std;

int str[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int str2[12]={31,28,31,30,31,30,31,31,30,31,30,31};

struct Year
{
int _Year;
int _month;
int _day;
};

int year(int _year)
{
if(_year%4==0 && _year%100!=0 || _year%100==0 && _year%400==0)
{
return str[12];
}
else
return str2[12];
}

int main()
{
int day,num=0,temp;
Year a;
while(cin >> a._Year >> a._month >> a._day)
{
year(a._Year);
if(year(a._Year)==str[12])
{
for(temp=0;temp<a._month;temp++)
num+=str[temp];
num+=a._day;
cout << num << endl;
}
if(year(a._Year)==str2[12])
{
for(temp=0;temp<a._month;temp++)
num+=str2[temp];
num+=a._day;
cout << num << endl;
}
}
return 0;
}

看看这个我写的,呵呵。有用的话你看看,不难的,思路很清楚的

#6
福尔摩斯2007-07-27 10:52
定义结构体是浪费
#7
lovelosegirl2007-07-27 16:38
额~~谢谢~!今天上课要学数组啦~!努力
#8
HJin2007-07-27 17:49
回复:(lovelosegirl)【初学者求助】一个简单的问题...

C source file. No optimization is applied.

No arrays are used.


======================================================

#include <stdio.h>

/**
Jan. 1 is the 1st day of each year.
January is 1st month.
Day 1 is 1st day.
*/
int dayCounting(int year, int month, int day)
{
int loopYear = (year % 4==0 && year%100!=0) || (year % 400 == 0);
int whichDay = 1;
int daysOfFeb = (loopYear ? 29 : 28);
switch(month)
{
case 1:
whichDay = day;
break;
case 2:
whichDay = 31 + day;

break;
case 3:
whichDay = 31 + daysOfFeb + day;

break;
case 4:
whichDay = 31 + daysOfFeb + 31 + day;

break;
case 5:
whichDay = 31 + daysOfFeb + 31 + 30 + day;

break;
case 6:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + day;

break;
case 7:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ day;

break;
case 8:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ day;

break;
case 9:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + day;

break;
case 10:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + 30 + day;

break;
case 11:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + 30 + 31+ day;

break;
case 12:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + 30 + 31+ 30 + day;

break;
}

return whichDay;
}

int main()
{
printf("%d\n", dayCounting(2007, 7, 28));

return 0;
}

#9
blueboy820062007-07-27 18:41

高!!

#10
野比2007-07-28 18:48
HJin...竟然玩阴的...
#11
し七月ご2007-07-29 00:55

#include<iostream>
using namespace std;
void main ()
{
int year ,int month,int day;
cout<<"Please input the date:\n";

cin>>year>>month>>day;
bool loopyear= (year%4==0&&year%400!=0)||(year%400==0);
int February=(loopyear?29:28);
int whichday;
swich(month)
{
case 1 :whichday=day;break;
case 2 :whichday=31+February;break;
case 3 :whichday=31+February+day;break;
case 4 :whichday=31+February+31+day;break;
case 5 :whichday=31+February+31+30+day;break;
case 6 :whichday=31+February+31+30+31day;break;
case 7 :whichday=31+February+31+30+31+30+day;break;
case 8 :whichday=31+February+31+30+31+30+31+day;break;
case 9 :whichday=31+February+31+30+31+30+31+31+day;break;
case 10 :whichday=31+February+31+30+31+30+31+31+30+day;break;

case 11 :whichday=31+February+31+30+31+30+31+31+30+31+day;break;
case 12 :whichday=31+February+31+30+31+30+31+31+30+31+30+day;break;
}


cout<<"Now the date is :"<<year<<"年"<<month<<"月"<<day<<"日\n"endle;
cout<<"Now this is the"<<whichday<<"day of the year";

}

请教哪有错?

1