前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2017百度之星初赛(A):今夕何夕

2017百度之星初赛(A):今夕何夕

作者头像
mathor
发布2018-07-24 15:31:46
2990
发布2018-07-24 15:31:46
举报
文章被收录于专栏:mathormathor
image
image
思路

 基姆拉尔森公式的应用,注意一下2月29号的情况

代码语言:javascript
复制
import java.util.Scanner;
public class Main {
    public static int day(int y,int m,int d) {
        if(m == 1 || m == 2) {
            m += 12;
            y -= 1;
        }
        int w=(d + 2*m + 3*(m + 1)/5 + y + y/4 - y/100 + y/400+ 1) % 7;
        return w;
    }
    public static boolean runnian(int a) {
        if((a % 4 == 0 && a % 100 != 0) || a % 400 == 0)
            return true;
        return false;
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int t,y,m,d;
        t = cin.nextInt();
        while((t--) != 0) {
            String str = cin.next();//录入字符串
            String[] strs = str.split("-");//使用正则表达式进行分割
            y = Integer.parseInt(strs[0]);
            m = Integer.parseInt(strs[1]);
            d = Integer.parseInt(strs[2]);
            boolean flag = false;
            if(m == 2 && d == 29)
                if(runnian(y))
                    flag = true;
            int x = day(y,m,d);
            for(int i = y + 1;i <= 10000;i++) {
                if(flag) {
                    if(day(i,m,d) == x && runnian(i)) {
                        System.out.println(i);
                        break;
                    }
                } else {
                    if(day(i,m,d) == x) {
                        System.out.println(i);
                        break;
                    }
                }
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档