| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
'''题目:写一个程序,判断给定年份是否为闰年 
(这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年)''' 
 
 
 
# 下面是我自己的思路,好像这样才更直白的体现题目描述吧? 
while True: 
    year = input('请输入要判断的年份:') 
    if year.isdigit(): 
        year = int(year) 
        if year % 4 == 0 and year % 100 != 0: 
            print(year,'是闰年') 
        elif year % 400 == 0: 
            print(year,'是闰年') 
        else: 
            print(year,'不是闰年') 
    else: 
        print('输入类型有误,只能输入数字!') 
 
 
# 下面是小甲鱼的答案,思路理解不了,请大神指点 
 
temp = input('请输入一个年份:') 
while not temp.isdigit(): 
    temp = input("抱歉,您的输入有误,请输入一个整数:") 
 
year = int(temp) 
if year/400 == int(year/400):  # 为什么要这样判断? 
    print(temp + ' 是闰年!') 
else: 
    if (year/4 == int(year/4)) and (year/100 != int(year/100)):  # 为什么要这样判断? 
        print(temp + ' 是闰年!') 
    else: 
        print(temp + ' 不是闰年!') 
 
小甲鱼只是提供了另一种判断是否整除的方法:当a/b的结果与a/b强制转成整数相等,就说明a/b是可以整除的。 
如果认同我的回答,给个最佳答案呗!    
 
 
 |   
 
 
 
 |