杭电oj —— 2005(Java代码 往后)

import java.util.Scanner;

/*
 * 给定一个日期,输出这个日期是该年的第几天。*/
public class HDU_oj2005 {
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		// 数组初始化 每个月的天数
		int m[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		while (sn.hasNext()) {
			String s = sn.nextLine(); // 定义一个字符串变量并输入它
			String[] str = s.split("/"); // 把我们输入的字符串用/分开并存储在str数组中
			int yy = Integer.parseInt(str[0]);
			int mm = Integer.parseInt(str[1]);
			int dd = Integer.parseInt(str[2]);

			int flag = 0; // 表示闰年的标记

			// 是闰年
			if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) {
				flag = 1;
			}

			int ans = 0; // 定义统计天数的变量
			for (int i = 0; i < mm - 1; i++) {
				ans = ans + m[i];
			}
			ans = ans + dd;
			if (flag == 1 && mm > 2) {
				ans++;
			}
			System.out.println(ans);
		}

	}
}

记得提交的时候类名写 Main

猜你喜欢

转载自blog.csdn.net/LiLi_code/article/details/87453348