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

问题描述
  编写函数求某年某月某日(**** ** **)是这一年的第几天 。提示:要考虑闰年,闰年的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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值