我的第三個 USACO Training--Friday the Thirteenth


題目:

Friday the Thirteenth

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 positive and will not exceed 400.

Note that the start year is NINETEEN HUNDRED, not 1990.

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.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34


因為1900年1月1號是星期一,可以算出各個月距離這一天的天數,然后模七。最后這個題目要注意的是最后輸出 每個數有空格,最后一個輸出要換行。

我的代碼


/*
ID: yang4521
LANG: JAVA
TASK: friday
*/
import java.io.*;
import java.util.*;
class friday {
	
	public static boolean isleapyear(int year)
	{	
		if((year%4==0)&&(year%100!=0))
        {
            return true;
        }
        else if(year%400==0)
        {
            return true;
        }else
        {
            return false;
        }
		
		
	}
	public static void main(String[] args) throws IOException {
		
	    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());
	    int	count[]=new int[7];//These array represent the number of times the 13th falls on  Sunday, Monday, Tuesday, ..., Friday,Saturday
	    int days=0;//days of one month
	    int currentYear=1900;
	    int m;
	    int allDays=0;
	    boolean isLeapYear=false;
	    for(int i=0;i<N;i++)
	    {	
	    		
	    	isLeapYear=isleapyear(currentYear);
	    	 for(int j=1;j<=12;j++)
	    	 {
	    		 
	    		 switch(j)
	    		 {
	    		 case 4:
	    		 case 6:
	    		 case 9:
	    		 case 11:
	    			 days=30;
	    			 break;
	    		 case 2:
	    			 if(isLeapYear)
	    			 {
	    				 days=29;
	    			 }
	    			 else
	    				 days=28;
	    			 break;
	    		 default:
	    			 days=31;
	    			 
	    		  }
	    		// int a=allDays+1;
	    		 count[(allDays+13)%7]++;
	    		 allDays+=days;

	    	 }
	    	
	    	
	    	 currentYear++;
	    }
	    out.print(count[6]+" ");
	    for(int i=0;i<5;i++)
   		 out.print(count[i]+" ");
		 out.println(count[5]);
	    out.close();  // close the output file
	    System.exit(0);
	  }

}

官網:

Friday the Thirteenth
Russ Cox

Brute force is a wonderful thing. 400 years is only 4800 months, so it is perfectly practical to just walk along every month of every year, calculating the day of week on which the 13th occurs for each, and incrementing a total counter.

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int
isleap(int y)
{
    return y%4==0 && (y%100 != 0 || y%400 == 0);
}

int mtab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/* return length of month m in year y */
int
mlen(int y, int m)
{
    if(m == 1)    /* february */
        return mtab[m]+isleap(y);
    else
        return mtab[m];
}

void
main(void)
{
    FILE *fin, *fout;
    int i, m, dow, n, y;
    int ndow[7];

    fin = fopen("friday.in", "r");
    fout = fopen("friday.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d", &n);

    for(i=0; i<7; i++)
        ndow[i] = 0;

    dow = 0;    /* day of week: January 13, 1900 was a Saturday = 0 */
    for(y=1900; y<1900+n; y++) {
        for(m=0; m<12; m++) {
            ndow[dow]++;
            dow = (dow+mlen(y, m)) % 7;
        }
    }

    for(i=0; i<7; i++) {
        if(i)
            fprintf(fout, " ");
        fprintf(fout, "%d", ndow[i]);
    }
    fprintf(fout, "\n");

    exit(0);
}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com