我用1表示星期一,2表示星期二等。
当我将年份值设为1时,它会返回1 2 1 3 1 2 2
,而根据USACO评分者,它应该返回2 1 1 3 1 2 2
。
这个序列意味着在一年的时间内,2个星期六的日期为13日,1个星期日的日期为13日等。
import java.io.*;
import java.util.*;
class friday {
static boolean isLeapYear(int year) {
if(year%4==0){
if(!(year%100==0))
return true;
else if(year % 400 == 0)
return true;
return false;
}
return false;
}
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
BufferedReader f = new BufferedReader(new FileReader("friday.in")); // input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
int n = Integer.parseInt(f.readLine()); //number of years
int[] count = new int[8];
for(int i = 1; i < 8; i++)
count[i] = 0;
for(int i = 1900,firstDayOfTheYear=1; i < 1900+n; i++) {
for(int month=1, monthDayOne = firstDayOfTheYear; month <= 12; month++) {
if(monthDayOne > 0 && monthDayOne <= 2)
count[8-monthDayOne]++;
else
count[monthDayOne-2]++;
if(month==2){
if(isLeapYear(i))
monthDayOne++;
}
else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
monthDayOne += 3;
else
monthDayOne += 2;
monthDayOne = monthDayOne > 7 ? monthDayOne%7: monthDayOne; //recalibrate
}
firstDayOfTheYear++;
firstDayOfTheYear = firstDayOfTheYear > 7? firstDayOfTheYear % 7 : firstDayOfTheYear;
}
//In the order specified
out.print(count[6]+" ");
out.print(count[7]+" ");
out.print(count[1]+" ");
out.print(count[2]+" ");
out.print(count[3]+" ");
out.print(count[4]+" ");
out.print(count[5]+" \n");
out.close();
System.exit(0);
}
}
答案 0 :(得分:0)
使用switch case语句,这将使您的逻辑更容易理解