USACO基础第二题Friday the Thirteenth

用吉大的模板貌似是错的,反正在我自己电脑上就没正确。做水题还用模板,作孽呀。不管怎么说,用模板还是很方便,要是按照我以前的做法,求哪天是星期几真没这么简单,只能佩服那些牛人们。记得大一学c语言的时候有这么个题好像好多人都没做出来,我是写出来了,但是好像用了几十行,原来五行以内就写完了。acm确实总会有让人吃惊的地方。

/*
ID: like_091
PROG: friday
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

struct date{
 int year, month, day;
};
//返回指定日期是星期几
int weekday(date a)
{
 int tm = a.month >= 3 ? (a.month - 2) : (a.month + 10);
 int ty  = a.month >= 3 ? a.year : (a.year - 1);
 //周日返回0,周一返回一
 return (ty + ty / 4 - ty / 100 + ty / 400 + (int)(2.6 * tm - 0.2) + a.day) % 7;
}

int main(void)
{
 ifstream cin("friday.in");
 ofstream cout("friday.out");
 int n, a, day[8];
 date x;
 while (cin>>n)
 {
  for (int i = 0; i < 8; i++)
   day[i] = 0;
  for (int k = 1900; k < 1900 + n; k++)
  {
   for (int tem = 1; tem <= 12; tem++)
   {
    x.day = 13;
    x.month = tem;
    x.year = k;
    a = weekday(x);
    day[a]++;
   }
  }
  for (int j = 0; j <= 6; j++)
  {
   cout<<day[(j + 6) % 7];
   if (j != 6)cout<<" ";//如果多输一个空格在后面都wa
  }
  cout<<endl;
 }
 return 0;
}
posted @ 2011-02-18 16:03  like@neu  阅读(230)  评论(0编辑  收藏  举报