首页 > 编程学习 > 蓝桥试题 算法训练 天数计算 JAVA

蓝桥试题 算法训练 天数计算 JAVA

发布时间:2022/2/7 9:33:53

问题描述
  编写函数求某年某月某日(**** ** **)是这一年的第几天 。提示:要考虑闰年,闰年的2月是29天(闰年的条件:是4的倍数但不是100的倍数,或者是400的倍数)。编写主函数,输入年月日,测试该函数并输出结果。
输入格式
  按“yyyy mm dd"的格式输入年月日
输出格式
  输出一个整数形式的计算结果
样例输入
1990 5 10
样例输出
130
数据规模和约定
  根据实际输入合法的日期
  
思路:用数组特性,然后直接+=出来每个月的天数。

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		int moth = sc.nextInt();
		int days = sc.nextInt();
		int[] arr = new int[13];
		boolean f = false;
		int ans = 0;
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {      // 判断是否是闰年
			f = true;
		}
		if (f) {
			arr[2] = 29;
		} else {
			arr[2] = 28;
		}
		for (int i = 1; i < arr.length; i++) {
			if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10  // 31天的月分
					|| i == 12) {
				arr[i] = 31;
			} else {
				arr[i] = 30;
			}
		}
		for (int i = 1; i < moth; i++) {     // 枚举
			ans += arr[i];                   // +=出数组的天数
		}
		ans += days;                         // 最后加上用户输入的天数
		System.out.println(ans);             // 输出结果
	}
}

思路:下面这个用switch ,好久没用到switch了,都手生了,差点不会写了=。=#


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		int moth = sc.nextInt();
		int days = sc.nextInt();
		int ans = 0;
		for (int i = 1; i < moth; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				ans += 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				ans += 30;
				break;
			case 2:
				if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
					ans += 29;
				} else {
					ans += 28;
				}
			default:
				break;
			}
		}
		System.out.println(ans+days);
	}
}

思路:下面这个纯暴力,能看到这里的都是真爱!!

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		int moth = sc.nextInt();
		int days = sc.nextInt();
		int month = 28;
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			month = 29;
		}
		if (moth == 1)
			System.out.println(days);
		if (moth == 2)
			System.out.println(days + 31);
		if (moth == 3)
			System.out.println(days + month + 31);
		if (moth == 4)
			System.out.println(days + month + 31 + 30);
		if (moth == 5)
			System.out.println(days + month + 31 + 30 + 31);
		if (moth == 6)
			System.out.println(days + month + 31 + 30 + 31 + 30);
		if (moth == 7)
			System.out.println(days + month + 31 + 30 + 31 + 30 + 31);
		if (moth == 8)
			System.out.println(days + month + 31 + 30 + 31 + 30 + 31 + 31);
		if (moth == 9)
			System.out.println(days + month + 31 + 30 + 31 + 30 + 31 + 31 + 30);
		if (moth == 10)
			System.out.println(days + month + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
		if (moth == 11)
			System.out.println(days + month + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
		if (moth == 12)
			System.out.println(days + month + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31);
	}
}

小剧场:行色匆匆,从不回望。Hurry, never look back.

Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000