Count the Number of Feb 29

  This is a problem cited from the qualification round of Beauty of Programming 2015, which demands counting the number of Feb 29 between the two given dates (both inclusively):

  

  My solution is as follow (it may seem a little clumsy):

 1 import java.util.*;
 2
 3 public class Main {
 4     public static Scanner in;
 5
 6     public static boolean isLeap(int year) {
 7         return ( year%400==0)||(year%100>0&&year%4==0);
 8     }
 9     public static boolean sameYearBefore(String month,int day,int year) {
10         // Whether there‘s a Feb 29 before the given date in the same year
11         if (!isLeap(year)) {
12             return false;
13         } else if (month.equals("February") && day==29) {
14             return false;
15         } else {
16             return !month.equals("January")&&!month.equals("February");
17         }
18     }
19     public static int sameCycleBefore(String month,int day,int year) {
20         // The number of leap years in the same 400-year cycle
21         //        before the given date (0<=year<400)
22         int val = (year+3)/4;
23         if (year>0) {
24             val -= (year-1)/100;
25         }
26         if (sameYearBefore(month,day,year)) {
27             return val+1;
28         } else {
29             return val;
30         }
31     }
32     public static void main(String[] args) {
33         in = new Scanner(System.in);
34         int time = in.nextInt();
35         for (int t = 0;t<time;t++) {
36             int val = 0;
37             String month = in.next();
38             String tmp = in.next();
39             int day = Integer.parseInt(tmp.substring(0,tmp.length()-1));
40             int year1 = in.nextInt();
41             val -= sameCycleBefore(month,day,year1%400);
42             month = in.next();
43             tmp = in.next();
44             day = Integer.parseInt(tmp.substring(0,tmp.length()-1));
45             int year2 = in.nextInt();
46             val += sameCycleBefore(month,day,year2%400);
47             val += 97*(year2/400-year1/400);
48             if (isLeap(year2)&&month.equals("February") && day==29) {
49                 val++;
50             }
51             System.out.println("Case #"+(t+1)+": "+val);
52         }
53         in.close();
54     }
55 }

========================= 旧地如重游月圆更寂寞 =========================

  Actually, this problem may be not worth mentioning, but what I want to talk about is an application of integer division in programming.

  To distribute n elements into m groups as evenly as possible, group j should contain (n+m-1-j)/m elements (if we want the group with a smaller index would have relatively more elements). In this way, the group with the largest element number would contain (n+m-1)/m elements (the ceiling of n over m) and the group with the smallest element number would have n/m elements (the floor of n over m), which indicates that the element number difference between two groups won‘t be greater than 1. Of course, such solution is also based on the fact that the summation of all those (n+m-1-j)/m is equal to n, which I learned last summer from Donald Knuth‘s book Concrete Mathematics:

  If we interleave the elements from an array into groups, there will be a mapping from the original position of an element into its current position: group[j][i] is initially at array[i*m+j] and array[pos] is currently at group[pos%m][pos/m].

  有点脑残,仅供娱乐。

时间: 2024-10-10 08:17:46

Count the Number of Feb 29的相关文章

uniapp报错:vue.runtime.esm.js:619 [Vue warn]: Invalid prop: type check failed for prop &quot;count&quot;. Expected Number with value 1, got String with value &quot;1&quot;.

这是组件内报错,将Type类型改为[Number, String]即可 props: { count: { type: Number, default: 0 }, } 改为 props: { count: { type: [Number, String], default: 0 }, } 原文地址:https://www.cnblogs.com/wanggang2016/p/12388382.html

Count the number of occurrences in a sorted array

Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr

OpenCV count the number of connected camera 检测连接的摄像头的数量

有时候在项目中我们需要检测当前连接在机子上的摄像头的数量,可以通过下面的代码实现,其中连接摄像头的最大数量maxCamNum可以任意修改: /** * Count current camera number */ int countCamera() { int maxCamNum = 5; int count = 0; for(int device = 0; device < maxCamNum; device++) { CvCapture* capture; if (_capture[devi

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字

[LintCode] Count of Smaller Number before itself

Count of Smaller Number before itself Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is smaller than it

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

USACO·2012·Feb Bronze &amp;&amp; 2009·Open Gold

Rope Folding [Brian Dean, 2012] 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John has a long rope of length L (1 <= L <= 10,000) that he uses for various tasks around his farm. The rope has N knots tied into it at various distinct locations (1 <= N <=

Leetcode 200. Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[LeetCode] 200. Number of Islands 解题思路

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by