首页
社区
课程
招聘
[求助]关于日历的编程题目
发表于: 2011-1-30 15:17 4961

[求助]关于日历的编程题目

2011-1-30 15:17
4961
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 non-negative and will not exceed 400.

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

下面是我写的程序
请高手帮我看一下

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

int num(int day);
bool lastleap(int year);
bool currentleap(int year);
int day_num(int daynum,bool b);
int date_13th( int daynum,bool b);

static int Sunday=0,Monday=0,Tuesday=0,Wednesday=0,Thursday=0,Friday=0,Saturday=0;

int main() {
        bool b;
        int daynum=0;
        int year=1900;
        ofstream fout ("friday.out");
        ifstream fin ("friday.in");
        int x;
        fin >> x;
       
        if(x>1)

        {
                daynum=0;
                date_13th(daynum,0);
               
                for (int j=0;j<x-2;j++)
                {       
                        year=1901+j;
                        b=lastleap(year);
                        daynum=day_num(daynum,b);
                        b=currentleap(year);
                        date_13th(daynum,b);
                        cout<<"j"<<j<<endl;
                        cout<<year<<endl;
                        cout<<daynum<<endl;
                        cout<<Saturday<<" "<<Sunday<<" "<<Monday<<" "<<Tuesday<<" "<<Wednesday<<" "<<Thursday<<" "<<Friday<<endl;
                       
                       
                }       
                        year++;
                        b=lastleap(year);
                        daynum=day_num(daynum,b);
                        b=currentleap(year);
                        date_13th(daynum,b);
                        cout<<year<<endl;
                        cout<<daynum<<endl;
                        cout<<Saturday<<" "<<Sunday<<" "<<Monday<<" "<<Tuesday<<" "<<Wednesday<<" "<<Thursday<<" "<<Friday<<endl;
                       
                       
        }
        else
        {       
                daynum=0;
                date_13th(daynum,0);
               
               
        }

        fout<<Saturday<<" "<<Sunday<<" "<<Monday<<" "<<Tuesday<<" "<<Wednesday<<" "<<Thursday<<" "<<Friday<<endl;
        return 0;
}
int date_13th( int daynum,bool b)
{       
        int a;
        int date[12]={13,44,72,103,133,164,194,225,256,286,317,347}        ;
       
        if (b)
        {       
                for (int i=0;i<2;i++)
                {
                        a=(daynum+date[i])%7;
                        num(a);
                }

                for (int i=2;i<12;i++)
                {
                        a=(daynum+date[i]+1)%7;
                        num(a);
                }

        }
        else
        {
                for (int i=0;i<12;i++)
                {
                        a=(daynum+date[i])%7;
                        num(a);
                }
        }
       

        return 0;
       

}
int num(int day)
{       
        int DAY=day;
        switch (DAY)
                {
                case 0:
                        Sunday++;
                        break;
                case 1:
                        Monday++;       
                                break;
                case 2:
                        Tuesday++;
                                break;
                case 3:
                        Wednesday++;
                                break;

                case 4:
                        Thursday++;
                                break;

                case 5:
                        Friday++;
                                break;

                case 6:
                        Saturday++;
                                break;

               
                }
       
       
       
        return 0;
}

bool lastleap(int year)
{       
        bool b;
        if(((year-1)%400==0)||((year-1)%100!=0&&(year-1)%4==0))
        {
                b=true;
        }
        else
        {
                b=false;
        }               
                       
               

        return b;
}
bool currentleap(int year)//leap year
{
                bool b;
        if((year%400==0)||(year-1%100!=0&&year%4==0))
        {
                b=true;
        }
        else
        {
                b=false;
        }               
                       
               

        return b;
       
}
int day_num(int daynum,bool b)//天数
{
        if(b)
        {       
                daynum=366+daynum;
        }
        else
        {
                daynum=365+daynum;

        }       
        return daynum;
}

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (5)
雪    币: 233
活跃值: (285)
能力值: ( LV12,RANK:270 )
在线值:
发帖
回帖
粉丝
2
USACO的题目?很少有人愿意看这种算法题代码,最好说思路或者把注释标一下~~
2011-1-30 15:32
0
雪    币: 1157
活跃值: (847)
能力值: ( LV8,RANK:150 )
在线值:
发帖
回帖
粉丝
3
自己跑一下不就知道了,这还问人,难道自己没有一点调试能力吗?如果真的没有调试能力,那买红薯吧,那个好弄
2011-1-30 16:25
0
雪    币: 182
活跃值: (81)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
4
你这wnl写的够吓人的
2011-2-19 00:25
0
雪    币: 182
活跃值: (81)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
static int  m_array[2][13] =\
                              {
                                       {0,31,29,31,30,31,30,31,31,30,31,30,31},
                                       {0,31,29,31,30,31,30,31,31,30,31,30,31}

                               };
01.class function
02.{
03.        friend class Date;
04.public:
05.                int IsLeapYear(Date * koma);       //是否是闰年
06.                int CountLeapYear(Date * koma);//统计标准年到现在有多少年
07.                int PrintYear(Date * koma);          //打印
08.                int InputYear(Date * koma);        //输入
09.                int monthdays(Date * koma);      //每个月有多少天
////////////////////////////////////////////////////////////
10.                int AddYear(Date * koma);          //
11.                int DecYear(Date * koma);          //上下左右翻页
12.                int AddMonth(Date * koma);       //
13.                int DecMonth(Date * koma);      //
/////////////////////////////////////////////////////////
15.                int KeyMSG(Date * koma);    //键盘消息
16.};
17.class Date
18.{
19.public:
20.        Date():year(0),month(0),day(0){};
21.public:
22.        int year;
23.        int month;
24.        int day;
25.};当时写的,只找到一个头文件
2011-2-19 00:28
0
雪    币: 270
活跃值: (97)
能力值: ( LV8,RANK:140 )
在线值:
发帖
回帖
粉丝
6
着个USACO的吧。我也把代码贴上来。还是去年写的,现在不想搞这个了
/*
	ID:stackex1
	LANG:C
	PROG:friday
*/

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

int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

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

void work(int n, int f[])
{
	int i, j, s = 1;
	for(i = 0; i < n; ++i)
	{
		for(j = 1; j <= 12; ++j)
		{
			s += 12;
			++f[s % 7];
			if(j == 2 && is_leap_year(1900 + i))
				s += 29 - 12;
			else
				s += month[j] - 12;
		}
	}
}

int main(int argc, char **argv)
{
	int n, f[7], i;
	
	FILE *fin = fopen("friday.in", "r");
	FILE *fout = fopen("friday.out", "w");
	
	fscanf(fin, "%d", &n);
	memset(f, 0, sizeof(f));
	work(n, f);
	
	fprintf(fout, "%d %d", f[6], f[0]);
	fprintf(fout, " %d %d %d %d %d\n", f[1], f[2], f[3], f[4], f[5]);

	return 0;
}

2011-2-19 11:22
0
游客
登录 | 注册 方可回帖
返回
//