OJ刷题---1.1.3 Friday the Thirteenth 黑色星期五

来源:互联网 发布:诲女知之乎女的意思 编辑:程序博客网 时间:2024/05/22 12:27

 题目要求:



输入代码:

#include<iostream>#include<stdio.h>#include<cstdlib>using namespace std;int month[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};int ans[8]= {0};int n,end;void work(int n){    int y,m,d,i;    int w=1;    y=1900;    end=y+n-1;    m=d=1;    while(y!=end||m!=12||d!=31)//到end年12月31日退出循环    {        if(d==month[m]+1)//表示到月底,换下一月        {            d=1;            m++;        }        if(m==13)//表示到12月份,到下一年        {            y++;            m=1;        }        if((y%100!=0&&y%4==0)||y%400==0)//闰年的二月        {            month[2]=29;        }        else        {            month[2]=28;        }        if(d==13)//13号        {            ans[w]++ ;//13号在该周的次数加1        }        w ++ ;        w = (w - 1 ) % 7 + 1 ;//表示周几        d ++ ;    }    cout<<ans[6]<<" "<<ans[7]<<" ";    for(i=1; i<=4; i++)    {        cout<<ans[i]<<" ";    }    cout<<ans[5]<<endl;}int main(){    //freopen( "friday.in" ," r " ,stdin);    //freopen( "friday.out" ," w " ,stdout);    cin>>n;    work(n);    //fclose(stdin);//关闭文件    //fclose(stdout);//关闭文件    return 0;}

运行结果:



0 0
原创粉丝点击