Give the year and month, make a monthly calendar

Write a program, enter the year and month, and output the monthly calendar for that month.

Input format:

Enter the year and month in one line. The year range is (positive integer), month (1~12).

Output format:

Output the monthly calendar of the month. The daily output character width is 8, no space is set between each line and day, right-aligned. All blank parts are filled with space characters. Pay attention to the last day of the month without extra spaces and line breaks.

Input sample:

Here is a set of inputs. E.g:

2018 8
output sample:

The corresponding output is given here. For example: each date occupies 8 characters width.

 SUN     MON     TUE     WED     THU     FRI     SAT
                           1       2       3       4
   5       6       7       8       9      10      11
  12      13      14      15      16      17      18
  19      20      21      22      23      24      25
  26      27      28      29      30      31

Code:

#include "stdio.h"
int dd(int i,int y) {
    
    
	if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
		return 31;
	if(i==4||i==6||i==9||i==11)
		return 30;
	if(i==2) {
    
    
		if(y%4==0&&y%100!=0||y%400==0) {
    
    
			return 29;
		} else
			return 28;
	}
}
int main() {
    
    
	int y,m,i,j,d,s,r,ans=1;
	scanf("%d %d",&y,&m);
	for(i=1; i<=m-1; i++) {
    
    
		ans+=dd(i,y);
	}
	int ts=dd(m,y);
	s=y-1+(y-1)/4-(y-1)/100+(y-1)/400+ans;
	r=s%7;
	printf("     SUN");
	printf("     MON");
	printf("     TUE");
	printf("     WED");
	printf("     THU");
	printf("     FRI");
	printf("     SAT\n");
	for(i=0; i<=r-1; i++)
		printf("        ");
	for(d=1;d<=ts;d++){
    
    
		printf("%8d",d);
		if(r%7==6&&d!=ts)
		printf("\n");
		r++;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/Helinshan/article/details/109032991