JAVA CCF-201503-3 节日

欢迎访问我的CCF认证解题目录


题目描述

在这里插入图片描述

思路过程

思路倒是不复杂,直接计算出相差的天数然后再进行转换即可,不过要注意的是:如果一号是星期日,那么1-7号都算是第一个星期,我之前直接把2号当前第二个星期了,害我找了好久的错误。。。。样例也没给我说清楚,坑!!!!


代码

import java.util.Scanner;

public class Main{
	
	//每月的天数
	static int[] mouth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt(), b = in.nextInt(), c = in.nextInt();
		int y1 = in.nextInt(), y2 = in.nextInt();
		for ( int i = y1; i <= y2; i++ ) {
			/**
			 * 之前:第几周+(星期-第一天星期)+1
			 * 现在:第几周+(星期+7-第一天星期)%7+1
			 * 括号内要计算的是要前进几号,第一个算法是有负数的,即如果1号为星期日,2号就是第二个星期了
			 * 而第二个算法的话要到8号才算第二个星期,题目样例也没搞清楚。。。。坑!!!
			 */
			int week = week(i, a);//第一天的星期几
			int day = 1 + (c+7-week)%7 + 7*(b-1);
			if ( day >= 1 && day <= mouth_sum(i, a) ) {
				System.out.printf("%d/%02d/%02d\n", i, a, day);
			} else System.out.println("none");
			
		}
	}
	
	
	//判断是否为闰年
	public static boolean is( int year ) {
		return year%400 == 0 || ( year%4 == 0 && year%100 != 0);
	}
	
	//获取两个年份相差的天数
	public static int year( int left, int right ) {
		int sum = 0;
		for ( int i = left; i < right; i++ ) {
			if ( is(i) ) sum += 366;//left-right这一年有多少天
			else sum += 365;
		}
		return sum;
	}
	
	//获取两个月份相差的天数
	public static int mouth( int left, int right, int year ) {
		int sum = 0;
		for ( int i = left; i < right; i++ ) {
			sum += mouth_sum(year, i);
		}
		return sum;
	}
	
	//该年该月的第一天是星期几
	public static int week( int year, int mouth ) {
		int sum = 0;
		sum += year(1850, year);
		sum += mouth(1, mouth, year);
		sum = (sum+2)%7;//1850年1月1日为星期2
		if ( sum == 0 ) sum = 7;//0为7
		return sum;
	}
	
	//该月的天数
	public static int mouth_sum( int year, int m ) {
		if ( m != 2 ) return mouth[m];//不是二月
		else if ( is(year) ) return 29;//是二月且为闰年
		else return 28;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值