2018-12-08 acm日常 HDU

J - Problem J HDU - 2005

第几天?

给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71

开始学acm的时候不知道为什么老是wa,后面回来看才发现,要用循环输入;
对于中间的字符‘/’用scanf_s就可以不读取;
x也要放入循环内部进行初始化。

/*
miku saiko
*/
#include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
	int y, m, d;
	int a[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
	int b[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	while (scanf_s("%d/%d/%d", &y, &m, &d) != EOF)
	{
		int  x = 0;
		if (m > 0 && m < 13 && d < 32 && d>0 && y>0)
		{
			if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
			{
				x += d;
				for (int i = 0; i < m; i++)
				{
					x += a[i];
				}
			}
			else
			{
				x += d;
				for (int i = 0; i < m; i++)
				{
					x += b[i];
				}
			}
		}
		cout << x << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/gidear/p/10433292.html