Coding Everyday

為{幸葍}努か 提交于 2019-12-15 10:24:29

2019/10/12

洛谷【P1425】

#wrong
time = input().split(" ")
hours = int(time[2])-int(time[0])
minutes = 60-int(time[1]) + int(time[3])
print(str(hours-1)+" "+str(minutes))
  • 统一单位之后一切都好办
#right
time = "12 50 19 10".split(" ")
first = 60*int(time[0]) + int(time[1])
second = 60*int(time[2]) + int(time[3])
minus = second - first
#
print(str(minus//60),str(minus%60))
#

2019/12/13

Lintcode【Trailing Zeros】

  • 计算0的个数可以换成计算2和5的个数,进而转化成算5的个数
    def trailingZeros(self,n):
        # write your code here, try to do it without arithmetic operators.
        num = 0
        while n>0:
            num = num + n//5
            n = n//5
        return num

2019/12/14

LintCode【Digit Counts】

  • 将数字转化成字符串进行处理
    def digitCounts(self, k, n):
        # write your code here
        #
        str_k = str(k)
        #
        count = 0
        for i in range(n+1):
            for j in str(i):
                if j == str_k:
                    count = count+1
        return count

2019/12/15

PAT【1001 害死人不偿命的(3n+1)猜想】

n = int(input())
times = 0
#
if n == 1:
    print(0)
#
else:
    while n != 1:
        if n%2 == 0:
            n = n/2
            times +=1
        else:
            n = (3*n+1)/2
            times +=1
    print(times)

PAT【1002 写出这个数】

  • 数字转化成字符串处理
num = input()
sum = 0
d = {"1":"yi","2":"er","3":"san","4":"si","5":"wu","6":"liu","7":"qi","8":"ba","9":"jiu","0":"ling"}
for i in num:
    sum += int(i)
str_sum = str(sum)
iteration_times = len(str_sum)-1	
#格式化输出
for j in range(iteration_times):	#int不能迭代。。变量名iteration自然而然以为是迭代了
        print(d[str_sum[j]],end=" ")
print(d[str_sum[-1]],end="")
#
#现在看看自己三个星期前写的真的很搞笑
num = input()
sum = 0
d = {1:"yi",2:"er",3:"san",4:"si",5:"wu",6:"liu",7:"qi",8:"ba",9:"jiu",0:"ling"}

for i in num:
    number = number + int(i)

if number//100 != 0 :
    hundred = number//100
else:
    hundred = 0

if (number-hundred*100)//10 != 0 :
    decade = (number-hundred*100)//10
else:
    decade = 0
if (number-hundred*100-decade*10) != 0 :
    digit = number-hundred*100-decade*10
else:
    digit = 0

if hundred:
    print(d[hundred],end=" ")
if decade:
    print(d[decade],end=" ")
if digit:
    print(d[digit])
else:
    print(d[0])
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!