PAT basic1021-1025(Python版)

Python2环境:

1021:

from sys import exit
str = raw_input()

digit = []
for i in str:
    digit.append(i)

for i in range(10):
    if digit.count('%d' % i) > 0:
        print '%d:%d' % (i, digit.count('%d' % i))

exit(0)


1022:

from sys import exit
str = raw_input().split()

num = int(str[0]) + int(str[1])
d = int(str[2])

list = []
out = ''

if num == 0:
    out = '0'
while num != 0:
    list.append(num % d)
    num /= d

list.reverse()
for i in list:
    out += '%d' % i

print out    
exit(0)


1023:

from sys import exit
str = raw_input().split()
num = [int(str[i]) for i in range(len(str))]        
out = ''

flag = 0
for i in range(1, len(num)+1):
    if num[i] != 0:
        flag = i
        break
out += '%d' % flag
for i in range(len(num)):
    if i == flag:
        out += ('%s' % i) * (num[i]-1)
    else:
        out += ('%s' % i) * num[i]

print out        
exit(0)


1024:

from sys import exit
str = raw_input().split('E')

if str[1] != '':
    index = int(str[1])
else: 
    index = 0

num = str[0]
intPart = num.split('.')[0]
xsPart = num.split('.')[1]
out = ''

if num[0] == '-':
    out += '-'

if index == 0:
    out = str[0]
elif index > 0:
    out += '%d' % int(abs(int(intPart)))
    if len(xsPart) <= index:
        out += xsPart
        out += '0' * (index - len(xsPart))
    else:
        out += xsPart[:index]
        out += '.'
        out += xsPart[index:]
else:
    out += '0.'
    out += '0' * (abs(index)-1)
    out += '%d' % int(abs(int(intPart)))
    out += xsPart

print out    
exit(0)


1025:

#!/usr/bin/python

from sys import exit
str = raw_input().split()
dict = {}
ListLen = int(str[1])        
revsLen = int(str[2])
list = []

for i in range(ListLen):
    tmp = raw_input().split()
    dict[tmp[0]] = tmp[1:]        

cur = str[0]                                  
for i in range(ListLen):
    list.append([cur, dict[cur][0], dict[cur][1]])
    cur = dict[cur][1]                                                  

count = 0
revList = []
while ListLen - count >= revsLen:
    y=list[count:count+revsLen]
    y.reverse()
    count += revsLen
    revList.extend(y)

revList.extend(list[count:])        
cur = revList[0][0]
count = 0

while ListLen - count >= revsLen:
    for i in range(revsLen-1):
        revList[count+i][2] = revList[count+i+1][0]
    
    if count + revsLen < ListLen:
        revList[count+revsLen-1][2] = revList[count+revsLen][0]

    count += revsLen

revList[-1][2] = '-1'
for i in range(ListLen):
    print revList[i][0], revList[i][1], revList[i][2]
                          
exit(0)

有一个case内存超限,还有一个没过,Python在时间效率和空间效率上还有待改进啊。。也没心情跳最后一个没过的case了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值