蓝桥杯 算法提高 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 }

 

转载于:https://www.cnblogs.com/search-the-universe/p/last_month_6.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值