C語言 打印日歷
#include <stdio.h>
main()
{ int i,j,k,year,week;
int day[]={31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0))
day[1]++;
week=((year-1)+(year-1)/4-(year-1)/100+(year-1)/400)%7;
for(i=0;i<12;i++)
{
printf("*Mon Tue Wed Thu Fri Sat Sun\n");
for(k=0;k<week;k++) printf(" ");
for(j=1;j<=day[i];j++)
{
printf(" %3d",j);
if(++week>=7)
{
printf("\n");
week=week%7;
}
if(i==10)
}
printf("\n");
}
}
輸出後十月末尾多了一行怎麼改?
最佳回答:
#include <stdio.h>
void main()
{
int i,j,k,year,week;
int day[]={31,28,31,30,31,30,31,31,30,31,30,31};
char month[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
printf("Input year you want to print:");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0)) day[1]++; //判斷是否閏年
week=(35+year+year/4-year/100+year/400)%7; //計算當年的1月1日是周幾的公式
for(i=0;i<12;i++)
{
printf("\n%s\n",month[i]); //輸出月份的英文
printf("Sun Mon Tue Wed Thu Fri Sat\n"); //輸出表頭
for(k=0;k<week;k++) printf(" "); //每月一號對齊它的星期數
for(j=1;j<=day[i];j++)
{
printf("%3d ",j);
if(++week>=7)
{
printf("\n");
week=week%7;
}
}
printf("\n");
}
}