• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
天道铸魂
博客园    首页    新随笔    联系   管理    订阅  订阅

蓝桥杯 算法提高 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\n",year,month);
47     else
48         printf("Calendar %d - 0%d\n",year,month);
49     printf("---------------------\n");
50     printf("Su Mo Tu We Th Fr Sa\n");
51     printf("---------------------\n");
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("\n");
64     }
65     printf("---------------------\n");
66 }
67 int main()
68 {
69     int y,m;
70     scanf("%d%d",&y,&m);
71     show(y,m);
72     return 0;
73 }

 

posted @ 2018-03-04 21:54  天道铸魂  阅读(529)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3