USACO 1.1.3 Friday the Thirteenth (friday)

來源:互聯網
上載者:User

        判斷閏年:(y%4==0 && y%100!=0) || (y%400==0)

        沒用演算法,一個個累計,還可改進。

 

/*<br />ID: gengjia1<br />LANG: C<br />TASK: friday<br />*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include <string.h><br />#include <assert.h><br />enum week{Sat, Sun, Mon, Tue, Wed, Thu, Fri};<br />typedef struct {<br />int year;<br />int month;<br />int day;<br />enum week weekday;<br />}time;<br />int main(void) {<br />FILE *fin = fopen ("friday.in", "r");<br />FILE *fout = fopen ("friday.out", "w");<br />int total_year, total_month = 12, total_day;<br />int i;<br />int counts[7] = {0};/*第一天是Saturday*/<br />fscanf (fin, "%d", &total_year);<br />assert(total_year <= 400);<br />/*時間初始化*/<br />total_year += (1900 - 1);<br />time t = {1900, 1, 1, Mon};<br />for(t.year = 1900; t.year <= total_year; t.year++) {<br />for(t.month = 1; t.month <= total_month; t.month++) {<br />switch(t.month){<br />case 1:<br />case 3:<br />case 5:<br />case 7:<br />case 8:<br />case 10:<br />case 12:<br />total_day = 31;<br />break;<br />case 4:<br />case 6:<br />case 9:<br />case 11:<br />total_day = 30;<br />break;<br />default:<br />{<br />if((t.year%4==0 && t.year%100!=0) || (t.year%400==0))<br />total_day = 29;<br />else<br />total_day = 28;<br />}<br />break;<br />}<br />for(t.day = 1; t.day <= total_day; t.day++) {<br />if (t.day == 13){<br />counts[t.weekday] +=1;<br />}<br />if((int)t.weekday >= 6)<br />t.weekday = Sat;<br />else<br />t.weekday = (enum week)((int)t.weekday + 1);<br />}<br />}<br />}<br />for(i = 0; i < 6; i++) {<br />fprintf (fout, "%d ", counts[i]);<br />}<br />fprintf (fout, "%d/n", counts[i]);<br />fclose(fin);<br />fclose(fout);<br />exit(0);<br />} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.