分支-13. 计算天数(15)

版权声明: https://blog.csdn.net/oFengWuYu1/article/details/27113033

本题要求编敲代码计算某年某月某日是该年中的第几天。

输入格式:

输入在一行中依照格式“yyyy/mm/dd”(即“年/月/日”)给出日期。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。

闰年的2月有29天。

输出格式:

在一行输出日期是该年中的第几天。

输入例子1:
2009/03/02
输出例子1:
61
输入例子2:
2000/03/02
输出例子2:

62

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String str = cin.nextLine();
		String[] strs = str.split("/");
		int year = Integer.parseInt(strs[0]);
		int month = Integer.parseInt(strs[1]);
		int day = Integer.parseInt(strs[2]);
		int[] months = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			months[1] = 29;
		}
		int result = 0;
		for (int i = 0; i < month - 1; i++) {
			result += months[i];
		}
		result += day;
		System.out.print(result);
	}
}


 
 

猜你喜欢

转载自www.cnblogs.com/ldxsuanfa/p/10716327.html
今日推荐