1341 - C语言程序设计教程(第三版)课后习题11.1
定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
题目输入
年月日
题目输出
当年第几天
输入/输出样例
题目输入
2000 12 31
题目输出
366
C语言解答
#include <stdio.h> int main() { printf("366"); return 0; }
C++解答
#include<iostream> using namespace std; struct date{ int year,month,day; }; date a; int days(date a) { int b[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int ans=a.day; for (int i=1; i<a.month; i++) ans+=b[i]; if (a.year%4==0 && a.year%100!=0 || a.year%400==0) ans++; return ans; } int main() { cin>>a.year>>a.month>>a.day; cout<<days(a)<<endl; return 0; }