第几天? HDU - 2005

Text Reverse
Time limit 1000 ms
Memory limit 32768 kB
OS Windows
Source C语言程序设计练习(一)

Problem Description
给定一个日期,输出这个日期是该年的第几天。

Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。

Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。

Sample Input
1985/1/20
2006/3/12

Sample Output
20
71

问题链接
HDU - 2005

问题简述:
给定一个日期,输出这个日期是该年的第几天。

问题分析:
注意大小月,闰年对日期造成的影响。

程序说明:
/部分通过输入字符串以实现,通过条件判断控制闰年二月份的天数。

#include <iostream>
using namespace std;
int main()
{
	int y, m, d;
	char e, f;
	while (cin >> y>>e>> m>>f>>d)
	{
		int a, b = 0, c[11] = { 0 };
		c[0] = 31, c[1] = 28, c[2] = 31, c[3] = 30, c[4] = 31, c[5] = 30, c[6] = 31, c[7] = 31, c[8] = 30, c[9] = 31, c[10] = 30;
		if (y % 400 == 0)
			a = 1;
		else if (y % 100 != 0 && y % 4 == 0)
			a = 1;
		else a = 0;
		if(m>2)
		b += a;
		for (int i = 0; m > 1; m--, i++)
			b += c[i];
		cout << b+d << endl;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012551/article/details/84924415