求C++程序 题目:输入某年某月某日,判断这一天是这一年的第几天?
#include<iostream>
using namespace std;
int main()
{
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int year,month,day;
int sum=0;
cout<<"请输入年月日"<<endl;
cin>>year>>month>>day;
if(month==1)
sum=day;
else if(month<=2)
sum=31+day;
else
{
if(year%100==0)
{
if(year%4==0)
{
for(int i=0;i<month-1;i++)
sum=sum+a[i];
sum=sum+1+day;
}
else
{
for(int k=0;k<month-1;k++)
sum=sum+a[k];
sum=sum+day;
}
}
else
{
if(year%4==0)
{
for(int i=0;i<month-1;i++)
sum=sum+a[i];
sum=sum+1+day;
}
else
{
for(int k=0;k<month-1;k++)
sum=sum+a[k];
sum=sum+day;
}
}
}
cout<<year<<"-"<<month<<"-"<<day<<"是今年的第"<<sum<<endl;
}
望楼主采纳。。
void main()
{
int year,month,day;
int months[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
cout<<"请输入你年月日:"<<"\n";
cin>>year>>month>>day;
for(int i=0;i<month;i++)
day=day+months[i];
if(month>2)
{
if((year%4==0&&year%100!=0)||year%400==0)//判断闰年
day=day+1;//2月份加一天
}
cout<<"这天是这一年的第:"<<day<<"天"<<"\n";
}