任意输入一个年,月,日,然后输出这个日期是这一年的第几天.
好久没练练基础了,写了个,你自己看看吧!
[CODE]#include<iostream>
using namespace std;
struct date{
int year;
int month;
int day;
};
bool isLeapYear(const date &d){
if (d.year % 100 == 0){
if (d.year % 400 != 0)
return false;
}
else{
if (d.year % 4 != 0)
return false;
}
return true;
}
bool isValid(const date &d){
if (d.year < 1 || d.month < 1 || d.month > 12 || d.day < 1)
return false;
switch (d.month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (d.day > 31)
return false;
break;
case 4: case 6: case 9 : case 11:
if (d.day > 30)
return false;
break;
case 2:
if (isLeapYear(d)){
if (d.day > 29)
return false;
}
else{
if (d.day > 28)
return false;
}
break;
}
return true;
}
int countDay(const date &d){
int record[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
if (d.month <= 2)
return record[d.month-1]+d.day;
else{
if (isLeapYear(d))
return record[d.month-1]+1+d.day;
else
return record[d.month-1]+d.day;
}
}
int main(){
int year,month,day;
cin >> year >> month >> day;
date d = {year,month,day};
if (!isValid(d)){
cout << "invalid" << endl;
system("pause");
return 0;
}
cout << countDay(d) << endl;
system("pause");
}[/CODE]
#include <iostream>
#include <numeric>
using namespace std;
const int arr[2][12+1]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
inline int isleapyear(int y)
{
return (y%4==0 && y%100!=0 || y%400==0);
}
int main()
{
int y,m,d;
scanf(\"%d %d %d\",&y,&m,&d);
printf(\"%d\n\",accumulate(arr[isleapyear(y)],arr[isleapyear(y)]+m-1,d));
}
比较简单~~~
代码:
#include<iostream>
using namespace std;
void main()
{
int year,month,day;
int days=0,day1;
int x=0,i;
cout<<"请输入一个日期"<<endl;
cin>>year>>month>>day;
for(i=1;i<=month-1;i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day1=31;break;
case 4:
case 6:
case 9:
case 11:day1=30;break;
case 2:if((year%4==0&&year%100!=0)||(year%400==0)) day1=29; //判断二月为大月或小月
else day1=28;
}
days=days+day1;
}
days=days+day;
cout<<"这是一年中的第"<<days<<"天!\n";
}
好久没练练基础了,写了个,你自己看看吧!
[CODE]#include<iostream>
using namespace std;
struct date{
int year;
int month;
int day;
};
bool isLeapYear(const date &d){
if (d.year % 100 == 0){
if (d.year % 400 != 0)
return false;
}
else{
if (d.year % 4 != 0)
return false;
}
return true;
}
bool isValid(const date &d){
if (d.year < 1 || d.month < 1 || d.month > 12 || d.day < 1)
return false;
switch (d.month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (d.day > 31)
return false;
break;
case 4: case 6: case 9 : case 11:
if (d.day > 30)
return false;
break;
case 2:
if (isLeapYear(d)){
if (d.day > 29)
return false;
}
else{
if (d.day > 28)
return false;
}
break;
}
return true;
}
int countDay(const date &d){
int record[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
if (d.month <= 2)
return record[d.month-1]+d.day;
else{
if (isLeapYear(d))
return record[d.month-1]+1+d.day;
else
return record[d.month-1]+d.day;
}
}
int main(){
int year,month,day;
cin >> year >> month >> day;
date d = {year,month,day};
if (!isValid(d)){
cout << "invalid" << endl;
system("pause");
return 0;
}
cout << countDay(d) << endl;
system("pause");
}[/CODE]
创建了struct 这种程序还没用过 都是直接上数组