算法笔记第三章3.1-3.3章节

3.1 简单模拟

【PAT B1001】

step = 0 #用于记录除法步数
n = int(input())
while n!=1:
    if(n%2 == 0):
        n = n/2else:
        n = (3*n+1)/2
    step+=1

print(step)

【PAT B1032】  第四组超时!

from collections import defaultdict
N = int(input())
ll = defaultdict(int)
MAX = 0
max_index = 0
for i in range(0,N):
    s = input()
    ls = s.split(' ')
    ll[ls[0]] += int(ls[1])
    if ll[ls[0]] > MAX:
        MAX = ll[ls[0]]
        max_index = ls[0]
print('{0} {1}'.format(max_index,MAX))

【codeup 1934】找x

n = int(input())
n_list = input().split(' ')

x = int(input())
tag = 0
for i in range(n):
    if x == int(n_list[i]):
        tag = 1
        print(i+1)
        break
if tag == 0:
    print(-1)

【PAT B1036】跟着奥巴马一起编程序

l = input().split(' ')
N = int(l[0])
C = l[1]
if N%2 != 0:
l = (N+1)/2
else:
l = (N/2)
for i in range(int(l)):
for j in range(N):
if i==0 or i==int(l)-1:
print(C,end='')
else:
if j == 0 or j == N-1:
print(C,end='')
else:
print(' ',end='')
print()

 

posted @ 2020-12-02 09:13  cola_cola  阅读(59)  评论(0编辑  收藏  举报