java - I Can't figure out why my logic is wrong for USACO Friday the 13th? -
i use 1 refer monday, 2 refer tuesday etc.
when give years value of 1, returns 1 2 1 3 1 2 2, whereas according usaco grader, should return 2 1 1 3 1 2 2.
this sequence means in span of year, 2 saturdays have date of 13th, 1 sunday has date of 13th etc.
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 randomaccessfile; it's 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 = 1; < 8; i++) count[i] = 0; for(int = 1900,firstdayoftheyear=1; < 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 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); } }
use switch case statement, make logic easier follow
Comments
Post a Comment