zoukankan      html  css  js  c++  java
  • python练习题2优化1

    #给出一个日期和一个数字,判断日期加上数字之后的新日期
    
    def day(date,num):
        #date=raw_input("(MM/DD/YY)>>")
        date=date.split("/")
        year1=int(date[-1])
        month1=int(date[0])
        day1=int(date[1])
        #num=raw_input("enter a number:")
        num=int(num)
        month_days=[31,28,31,30,31,30,31,31,30,31,30,31]
        
        if (int(date[2])%4==0 and int(date[2])%100!=0) or int(date[2])%400==0:
            month_days[1]=29
            years1=366
        else:
            years1=365
        days1=sum(month_days[:(month1-1)])+day1
        sumdays=days1+num
        if num>=0: #数字为正数或者0
            lis=[]
            if sumdays<=years1: #本年
                year2=year1
                print year2
                lis.append(year2)
                sumday=0
                for i,j in enumerate(month_days):
                    if sumday<sumdays:
                        sumday=sumday+int(j)
                    
                month2=i+1
                print month2
                lis.append(month2)
                marginday=sum(month_days[:i+1])-sumdays
                day2=month_days[i]-marginday
                print day2
                lis.append(day2)
                 
                print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])   
            else:  #不在本年
    
                lis=[]
                remain1=years1-days1
                remain2=num-remain1
                
                year2=year1+1
                if (year2%4==0 and year2%100!=0) or year2%400==0:
                    years2=366
                    month_days[1]=29   
                else:
                    years2=365
                    month_days[1]=28 
                    
                while remain2>years2:              
                    remain2=remain2-years2
                    year2=year2+1
                    if (year2%4==0 and year2%100!=0) or year2%400==0:
                        years2=366
                        month_days[1]=29   
                    else:
                        years2=365 
                        month_days[1]=28
                lis.append(year2)
                       
               
                for i,j in enumerate(month_days):
                    if remain2<=sum(month_days[:i+1]):
                        month2=i+1
                        lis.append(month2)
                        day2=remain2-sum(month_days[:i])
                        lis.append(day2)
                        break
                print type(lis)
                print lis
                print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])
        else: #num为负数的时候
            lis=[]
            remindays=0
            years2=years1
            year2=year1        
            if sumdays<=0:
                remindays=abs(sumdays)
                while remindays>=0:
                    year2=year2-1
                    
                    if (year2%4==0 and year2%100!=0) or year2%400==0:
                        remindays=remindays-366
                        month_days[1]=29
                    else:
                        remindays=remindays-365
                        month_days[1]=28
                lis.append(year2)
                print remindays  
                remindays=abs(remindays)
            else:
                remindays=sumdays
                lis.append(year1)
                
            print remindays
            for i,j in enumerate(month_days):
                if remindays<=sum(month_days[:i+1]):
                    month2=i+1
                    lis.append(month2)
                    day2=remindays-sum(month_days[:i])
                    lis.append(day2)
                    break
                
            print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])   
            
    day("3/1/2016",'365')    
  • 相关阅读:
    我理解的BFC
    框架设计—选择器模块
    eval 的使用与延展
    Git学习笔记
    CSS3选择器学习笔记
    JavaScript继承学习笔记
    JavaScript原型与原型链学习笔记
    javascript面向对象编程笔记
    GitHub图形界面使用笔记
    HTML5表单学习笔记
  • 原文地址:https://www.cnblogs.com/sunyucui/p/7097877.html
Copyright © 2011-2022 走看看