The following program was supposed to read a number of years from a file called "friday.in" then print out how many of the thirteenth were on each day from monday to sunday on to a file called "friday.out ". Here is the code which I used .
The above code takes a long time to give an output and uses much more memory.
Code:
import java.io.*;
import java.util.*;
public class friday{
public static void main(String args[]) throws IOException{
BufferedReader in = new BufferedReader(new FileReader("friday.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
int mon,tue,wed,thu,fri,sat,sun;
mon = tue = wed = thu = fri = sat = sun = 0;
int day = 13;
int year = 1900;
int years = Integer.parseInt(in.readLine());
while (year < year + years){
int numday;
int tot;
int month = 1;
switch (month){
case 1:tot = 31;break;
case 2:tot = (((year % 4)==0) || ((year % 100) == 0))?29:28;break;
case 3:tot = 31;break;
case 4:tot = 30;break;
case 5:tot = 31;break;
case 6:tot = 30;break;
case 7:tot = 31;break;
case 8:tot = 31;break;
case 9:tot = 30;break;
case 10:tot = 31;break;
case 11:tot = 30;break;
case 12:tot = 31;break;
default:tot = 31;
}
if (((year % 4)==0) || ((year % 100) == 0)){
numday = 366;
}
else{
numday = 365;
}
while (month <= 12){
int cat = day % 7;
if (cat == 1){
mon++;
}
else if (cat == 2){
tue++;
}
else if (cat == 3){
wed++;
}
else if (cat == 4){
thu++;
}
else if (cat == 5){
fri++;
}
else if (cat == 6){
sat++;
}
else{
sun++;
}
day += tot;
month++;
}
day = day % numday;
year++;
}
out.write(sat+" "+sun+" "+mon+" "+tue+" "+wed+" "+thu+" "+fri+"\n");
out.close();
System.exit(0);
}
}
Comment