Third USACO problem:
Is Friday the 13th really an unusual event?
"That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.
There are few facts you need to know before you can solve this problem:
January 1, 1900 was on a Monday.
Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please."
for (int l=0;l<monthDays[k];l++)
{
if (l==13)
// If you started off on day 1, your if (i == 13) statement would be fine.
// But you started it off as 0, so this should read if (i == 12)
Days: 0 1 2 3 4 5 6 7 8 9 10 11 12 <- this is the 13th day for the month in your program
1 2
for (int j=0;j<N;j++)
// The question says to start at the year 1900, so I find it odd that j = 0 here
1 2 3
totalDays=1;
...
totalDays++; // look at where this line of code currently is
1 2 3 4 5
int number=0;
number=totalDays%7;
daysNum[number]++;
// I simplified this into daysNum[totalDays%7];
// one line of code verses 3 = bonus
Hmm, I think that's it (may have changed something else to get it working - not sure). Fix those things, and it will work - if it doesn't, post your code again and I'll see what I can do.
I'm sorry, but I don't understand the middle 2 (2nd and 3rd) edits. Could you please clarify? I don't know what you mean...
And not to sound rude or anything, but could you please rush your response? I am in kind of a hurry.
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
int main()
{
int totalDays=1;
vector<int> monthDays(12);
vector<int> daysNum(7);
monthDays[0]=31, monthDays[1]=28, monthDays[2]=31, monthDays[3]=30, monthDays[4]=31, monthDays[5]=30, monthDays[6]=31,
monthDays[7]=31, monthDays[8]=30, monthDays[9]=31, monthDays[10]=30, monthDays[11]=31;
for (int years = 1900; years < 1920; years++) // The years the question wants is from 1900 to 1200
{
if (years % 4 == 0) // Much easier to read when the variable is called "years" instead of j
{
monthDays[1]=29; // You didn't really need a variable initialization to tell
// you how many days in the year there are. If February has 29 days, then bam, 366,
// else, 365
if (years % 100 == 0 && years % 400 != 0)
monthDays[1]=28;
}
else
monthDays[1]=28;
for (int month = 0; month < 12; month++)
{
for (int day = 0; day < monthDays[month]; day++)
{
totalDays++; // moved this up as well
if (day == 12) // you either need if day == 13 - 1, or day == 12, since you NEED
// to initialize int day to 0 due to the way arrays work ( [0] being the first value)
daysNum[totalDays % 7]++; // instead of initializing int number to
// totaldays % 7, I skipped the 2 lines of code and just put the number you get inside of
// daysNum[ in here ]
}
}
}
for (int j = 0; j < 7; j++)
cout << daysNum[j] <<" "; // I'm too lazy to open a text file to see the answers
// so I just displayed it to the screen
cin.get(); // so I can see the answers (you used system pause, but alot of people don't
// like that for whatever reason, guess its a bad habit to get into)
return 0;
}
Output:
36 33 34 33 35 35 34
I'll comment what changes I made to your program in a second.
Okay, so I know that I am bothering you a lot, and I feel stupid, but my code only gives me a blank window that does not get to system pause. This is my code...
Okay, so the USACO grading machine spat this out when I entered my program...
> Run 1: Execution error: Your program had this runtime error: Bad
syscall #32000174 (RT_SIGACTION) [email kolstad if you think this
is wrong]. The program ran for 0.000 CPU seconds before the error.
It used 3344 KB of memory.
------ Data for Run 1 ------
1
----------------------------
Can anyone tell me why/what this means?
Sounds like the grading machine doesn't like your use of system ("PAUSE"); - that's the only call to system I can see in your program, so replace it with cin.get(); and you should be fine (I could be wrong though, but try this out)