[Java] Friday the Thirteenth

/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: lincans1
LANG: JAVA
TASK: friday
*/
import java.io.*;
import java.util.*;

public class friday {
    
    
	private final int WEEK = 7;
	private final int MONTH = 12;
	public static void main (String [] args) throws IOException {
    
    
		new friday();
	}
	public friday () throws IOException {
    
    
		// Use BufferedReader rather than RandomAccessFile; it's much faster
		BufferedReader f = new BufferedReader(new FileReader("friday.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
		int N = Integer.parseInt(f.readLine());
		int days = 0, year = 1900;
		// res[0] is Sunday
		int[] res = new int[WEEK];
		for (int i = 0; i < N; i++) {
    
    
			for (int j = 1; j <= MONTH; j++) {
    
    
				res[(days + 13) % WEEK]++;
				switch (j) {
    
    
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
						days += 31;
						break;
					case 4:
					case 6:
					case 9:
					case 11:
						days += 30;
						break;
					case 2:
						if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
							days++;
						days += 28;
						break;
					case 12:
						days += 31;
						year++;
						break;
					}
			}
		}
		for (int i = 0; i < WEEK; i++) {
    
    
			out.print(res[(i + 6) % WEEK]);
			if (i != WEEK - 1) {
    
    
				out.print(" ");
			}
			else {
    
    
				out.println();
			}
		}
		out.close();
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41714373/article/details/111844627