zoukankan      html  css  js  c++  java
  • 蓝桥杯 算法提高 5-3日历

      这题算水题吧,但我还是放上来了,因为觉得自己写的代码很有美感(要点脸可以吗...)

    问题描述
      已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。为完成此函数,设计必要的辅助函数也是必要的。
    样例输入
    一个满足题目要求的输入范例。
    例:

    2050 3
    样例输出
    与上面的样例输入对应的输出。
    例:


    数据规模和约定
      输入数据中每一个数的范围。
      例:年 2007-3000,月:1-12。
    ---------分割线---------
     1 #include<stdio.h>
     2 int is_leap_year(int year)
     3 {
     4     return (year%4||year%100==0&&year%400!=0)?0:1;
     5 }
     6 int year_days(int year)
     7 {
     8     return is_leap_year(year)?366:365;
     9 }
    10 int month_days(int year,int month)
    11 {
    12     int days;
    13     if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    14         days=31;
    15     else if(month!=2)
    16         days=30;
    17     if(month==2)
    18     {
    19         if(is_leap_year(year))
    20             days=29;
    21         else
    22             days=28;
    23     }
    24     return days;
    25 }
    26 int week(int year,int month)
    27 {
    28     int day1=0;
    29     month--;
    30     while(month)
    31     {
    32         day1+=month_days(year,month);
    33         month--;
    34     }
    35     while(year!=2007)
    36     {
    37         year--;
    38         day1+=year_days(year);
    39     }
    40     return day1%7;
    41 }
    42 void show(int year,int month)
    43 {
    44     int day1,t,i;
    45     if(month>=10)
    46         printf("Calendar %d - %d
    ",year,month);
    47     else
    48         printf("Calendar %d - 0%d
    ",year,month);
    49     printf("---------------------
    ");
    50     printf("Su Mo Tu We Th Fr Sa
    ");
    51     printf("---------------------
    ");
    52     day1=week(year,month);
    53     t=day1+1;
    54     while(t%7||t)
    55     {
    56         printf("   ");
    57         t--;
    58     }
    59     for(i=1;i<=month_days(year,month);i++)
    60     {
    61         printf("%2d ",i);
    62         if((i+day1+1)%7==0||i==month_days(year,month))
    63             printf("
    ");
    64     }
    65     printf("---------------------
    ");
    66 }
    67 int main()
    68 {
    69     int y,m;
    70     scanf("%d%d",&y,&m);
    71     show(y,m);
    72     return 0;
    73 }
  • 相关阅读:
    SQLite Java Wrapper/JDBC Driver(收集)
    JAVA 并行运行(收集)
    log4net使用方法(转载)
    WMI服务故障,VBS脚本无法运行错误
    ArcEngine中UID使用资料收集
    使用 ArcGIS Engine Runtime 制作安装包(转载)
    Eclipse安装WindowBuilder Pro(转载)
    C#操作SQL Server数据库
    自动化测试 (三) Web自动化测试原理
    HTTP协议 (六) 状态码详解
  • 原文地址:https://www.cnblogs.com/search-the-universe/p/last_month_6.html
Copyright © 2011-2022 走看看