原题链接http://ace.delos.com/usacoprob2?a=4uwBVp28abk&S=beads
/*
ID: wconveyPROG: friday
LANG: C++
*/
#include<iostream>
#include<memory.h>
#include<cstdio>
using namespace std;
int is_leap_year(int x)
{
if(x%400==0||(x%4==0&&x%100!=0))
return 1;
else
return 0;
}
int main()
{
int week[7],month[13];
int n,i,j,temp1,temp;
for(i=1;i<=12;i++)
if(4==i||6==i||9==i||11==i)
month[i]=30;
else if(i==2)
month[i]=28;
else
month[i]=31;
freopen("friday.in","r",stdin);
freopen("friday.out","w",stdout);
scanf("%d",&n);
{
memset(week,0,sizeof(week));
temp=0;
for(i=0;i<n;i++)
{
if(is_leap_year(i+1900))
{
temp++;//ÿÄêµÄµÚÒ»ÌìÊÇÖܼ¸
temp1=0;
for(j=1;j<=12;j++)
{
week[(temp1+temp+13-1)%7]++;//ÿÔµĵÚ13ÌìÊÇÖܼ¸//!!!!-1
if(2==j) //ÊÇj²»ÊÇi
temp1+=29;
else
temp1+=month[j];
}
temp=(temp+366-1)%7;//ÿÄêµÄ×îºóÒ»ÌìÊÇÖܼ¸
}
else
{
temp++;//ÿÄêµÄµÚÒ»ÌìÊÇÖܼ¸
temp1=0;
for(j=1;j<=12;j++)
{
week[(temp1+temp+13-1)%7]++;
temp1+=month[j];
}
temp=(temp+365-1)%7;//ÿÄêµÄ×îºóÒ»ÌìÊÇÖܼ¸
}
}
printf("%d",week[6]);
for(i=0;i<6;i++)
printf(" %d",week[i]);
printf("\n");
}//******while
return 0;
}
数据不大 ,模拟一下就过了 ,时间复杂度 O(n^2),据说 用DP 的话,O(n), 可惜 不熟 ;
模拟的思路:在字符串的后面 ,再复制 两串,从中间s[n]开始模拟 到 s[2*n-1];