Java
import java.util.Scanner;
public class Homework {
public static void space(int a){
for(int i=0;i4;i++){
System.out.print(" "); }
} //输出所有占位的空格,每组数字4个字符
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int flag=0,now_flag=0;
System.out.println("********* 欢迎使用万年历 *********"); System.out.print("请输入要查询的年份:");
int years=input.nextInt();
int years_num=years-1900; //已经过去的年数
for(int i=1900;i;i++){
if(i%4==0&&i%100!=0||i%400==0)
flag+=1;
} //判断已经过去的年中有多少个闰年
System.out.print("请输入要查询的月份:");
int months=input.nextInt();
int M=0; //判断要输出的万年历中的月份有多少天
switch (months){
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
M=31; break;
case 2:
M=28; break;
case 4:case 6:case 9:case 11:
M=30; break; }
if(years%4==0&&years%100!=0||years%400==0){
now_flag=1; } //判断今年是否是闰年
int months_day=0; //判断今年已经过去了多少天
switch (months-1){
case 1:
months_day=31; break;
case 2:
months_day=59+now_flag; break;
case 3:
months_day=90; break;
case 4:
months_day=120; break;
case 5:
months_day=151; break;
case 6:
months_day=181; break;
case 7:
months_day=212; break;
case 8:
months_day=243; break;
case 9:
months_day=273; break;
case 10:
months_day=304; break;
case 11:
months_day=334; break; }
int days_num=years_num*365+flag+months_day; //计算从1990年到本月1日已经过去了多少天
System.out.println("一\t二\t三\t四\t五\t六\t日");
if(days_num%7==0){
System.out.println(); } //如果已经过去的天数%7==0,说明本月1日是周一,输出空行
space(days_num%7);
if(months==2)
M+=now_flag;
for(int i=1;i<=M;i++){
System.out.print(i+"\t");
if ((i%7+days_num)%7==0){
System.out.println(); }
} //输出中判断换行 }
}
Python
def space(a):
for i in range(a):
print("\t",end="")
def weekday():
week = ['一', '二', '三', '四', '五', '六', '日']
for i in week:
print("%s\t" % i, end="")
print()
month_day=[31,28,31,30,31,30,31,31,30,31,30,31]
month_day_add=[]
for i in range(len(month_day)):
sum=0
for j in range(i):
sum+=month_day[j]
month_day_add.append(sum)
# print(month_day_add) [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
print("{:*^27}".format(" 欢迎使用万年历 "))
year=int(input("请输入查询的年份:"))
month=int(input("请输入查询的月份:"))
runnian_num=0
for i in range(1900,year):
if (i%4==0 and i%100!=0) or i%400==0:
runnian_num+=1
num=(year-1900)*365+runnian_num+month_day_add[month-1]
weekday() #打印一二三四...
if num%7==0:
print()
space(num%7)
M=month_day[month-1]
if month==2 and ((year%4==0 and year%100!=0) or year%400)==0:
M+=1
for i in range(1,M+1):
print(i,"\t",end="")
if (i%7+num%7)%7==0:
print()