zoukankan      html  css  js  c++  java
  • 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
    
    
    
    
    
    由于看错题,可花了我好长一段时间。
    View Code
     1 /*
    2 ID:10239512
    3 PROG:friday
    4 LANG:C++
    5 */
    6
    7 #include<iostream>
    8 #include<fstream>
    9 using namespace std;
    10 ifstream fin("friday.in");
    11 ofstream fout("friday.out");
    12
    13 int n,day,days,year=1900,month,num[8]={0};int x[8]={0,1,3,5,7,8,10,12};
    14
    15 bool leap(int y){
    16 if((y%4==0&&y%100!=0)||y%400==0)
    17 return 1;
    18 return 0;
    19 }
    20 bool check(int y){
    21 int i;
    22 for(i=1;i<=7;i++)
    23 if(x[i]==y) return 0;
    24 return 1;
    25 }
    26
    27 int main()
    28 {
    29 int i,j,k,l;
    30 fin>>n;
    31 day=1;
    32 while(year<1900+n)
    33 {
    34 month=0;
    35 while(month<=11)
    36 {
    37 month++;days=1;
    38 if(month==2&&leap(year)!=1)
    39 {
    40 while(days<=28)
    41 {
    42 days++;
    43 day++;
    44 if(day>7) day=1;
    45 if(days==13) num[day]++;
    46 }
    47 continue;
    48 }
    49
    50 if(month==2&&leap(year)==1)
    51 {
    52 while(days<=29)
    53 {
    54 days++;
    55 day++;
    56 if(day>7) day=1;
    57 if(days==13) num[day]++;
    58 }
    59 continue;
    60 }
    61
    62 if(check(month))
    63 {
    64 while(days<=30)
    65 {
    66 days++;
    67 day++;
    68 if(day>7) day=1;
    69 if(days==13) num[day]++;
    70 }
    71 continue;
    72 }
    73
    74 if(!check(month))
    75 {
    76 while(days<=31)
    77 {
    78 days++;
    79 day++;
    80 if(day>7) day=1;
    81 if(days==13) num[day]++;
    82 }
    83 continue;
    84 }
    85
    86 }
    87 year++;
    88 }
    89
    90 fout<<num[6]<<" "<<num[7]<<" ";
    91 for(i=1;i<=4;i++)
    92 fout<<num[i]<<" ";
    93 fout<<num[5];
    94 fout<<endl;
    95 return 0;
    96
    97 }
  • 相关阅读:
    ASP.NET WebApi项目框架搭建(六):数据库ORM之Sqlsugar
    sqlsugar与数据库之间的相互操作
    C# SqlSugar框架的学习使用(一)SqlSugar简介及创建
    SqlSugar直接执行Sql
    在项目中迁移MS SQLServer到Mysql数据库,实现MySQL数据库的快速整合
    SqlSugar 简易操作数据库
    C# SqlSugar框架的学习使用(二) 类的生成及增删改查的应用
    使用开源框架Sqlsugar结合mysql开发一个小demo
    devops起源的各种ops概念
    STC8H开发(四): FwLib_STC8 封装库的介绍和注意事项
  • 原文地址:https://www.cnblogs.com/noip/p/2368676.html
Copyright © 2011-2022 走看看