Coding Everyday

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 = input().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])

PAT【1003 我要通过!】

正则表达式

#之前写的,总是非零返回。。
import re
str_to_be_match = input().split("\n")
for i in range(int(str_to_be_match[0])):
    if re.match(r'(A*)P(A+)T(A*)',str_to_be_match[i+1]):
        print("YES")
    else:
        print("NO")  

PAT【1006 换个格式输出整数】

#之前写的
num = int(input())
if num // 100 != 0:
    hunderd = num // 100
else:
    hunderd=0
if (num-100*hunderd)//10 != 0:
    decade = (num-100*hunderd)//10
else:
    decade = 0
if (num-hunderd*100-decade*10) != 0:
    digit = num-hunderd*100-decade*10
else:
    digit = 0
for i in range(hunderd):
    print("B",end="")
for i in range(decade):
    print("S",end="")
for i in range(digit):
    print(i+1,end="")
  • 判断数字的位数同样可以通过将数字转化成字符串处理
num = input()
if len(num)==3:
    print(int(num[-3])*"B",end="")
if len(num)>=2:
    print(int(num[-2])*"S",end="")
if num[-1]:
    for i in range(int(num[-1])):
        print(i+1,end="")

LeetCode【最长有效括号】

#只筛选出来了包含成对()的字符串;下次继续改
class Solution:
    def longestValidParentheses(self, s: str) -> int:
        ls = []
        num = 0
        for i in s:
            if i == "(":
                ls.append(i)
            else:
                if ls:
                    num+=1
                    ls.pop()
        return num*2  
 #  只筛选出来了连续出现的()的字符串;嵌套的不能筛选出来,很失败,下次继续改
    def longestValidParentheses(self, str_to_bematched: str) -> int:
        ls = []
        num = 0
        left_of_symbol_exist = False
        
        for i in str_to_bematched:
            if i == "(" and left_of_symbol_exist == False:
                ls.append(i)
                left_of_symbol_exist = True
            if i == ")" and left_of_symbol_exist == True:
                if ls:
                    num+=1
                    ls.pop()
                    left_of_symbol_exist = False
        return num*2

考试周,没时间搞CS

2019/12/27

#用python仿造栈数据结构
class Stack:
    def __init__(self):
        self.items() = []

    def push(self,item):
        self.items.append()

    def isEmpty(self):
        return self.items == []

    def pop(self,item):
        return self.items.pop()

    def size(self):
        return len(self.items)

你可能感兴趣的:(Coding Everyday)