本来4月9号的京东第一题很简单,但是因为对sys.stdin操作不熟,导致程序通不过。
补充注意:
牛客网官方输入案例适合于测试用例输入是单行
#python
import sys
try:
while True:
line = sys.stdin.readline().strip()
if line == '':
break
lines = line.split()
print int(lines[0]) + int(lines[1])
except:
pass
#python3
import sys
for line in sys.stdin:
a = line.split()
print(int(a[0]) + int(a[1]))
输入部分
python环境: 2.7.3
按照题目的意思:
第一行输入时N,然后剩余N行输入要处理的数字。
思路解释:
- 最外层while循环是应付牛客网环境,应付多个测试用例输入。(暂时未验证,如果普通题目这层可不用)
- try...except... 如果读取错误就跳出try到except
- 先用sys.stdin.readline().strip()读取第一行,然后再用以下循环读取指定的行数
for i in range(m):
print 'TDDO2'
line = sys.stdin.readline().strip()
中间遇到的坑,待解决
for line in sys.stdin:
读取遇到空行时就会出错到except
最终版的输入部分:
while 1:
print 'Start'
try:
while True:
a = []
print 'TODO: Start A CYCLE:',a
# print ''.join([sys.stdin.readline() for i in range(3)])
# for line in sys.stdin:
m = sys.stdin.readline().strip()
m = int(m)
for i in range(m):
print 'TDDO2'
line = sys.stdin.readline().strip()
if not line:
# break
break
else:
a.append(int(line))
print 'test a:',a
# a = sys.stdin.strip()
...
...
...
except:
print ' OK,the script is over\n\n'
附1: 字符拼接操作
方法1: 用reduce(lambda x,y: str(x)+str(y),L0)TODO:for line in sys.stdin: if 这里怎么判断空行或者方法2
方法2: “”.join(L)
拼接 数字的list L0, 要首先将数字转换成字符形成的list L=map(str,L0)
附2:京东笔试第一题
#codng=utf-8
'''
JD笔试题
算出一组数的是否分解成偶数和奇数的和,如果是,就显示,如果没有,就No
示范输入:
2
10
5
示范输出
5 2
No
'''
import sys
import math
while 1:
print 'Start'
try:
while True:
a = []
print 'TODO: Start A CYCLE:',a
# print ''.join([sys.stdin.readline() for i in range(3)])
# for line in sys.stdin:
m = sys.stdin.readline().strip()
m = int(m)
for i in range(m):
print 'TDDO2'
line = sys.stdin.readline().strip()
if not line:
# break
break
else:
a.append(int(line))
print 'test a:',a
# a = sys.stdin.strip()
# if a == '':
# print 'NULL'
# break
# a = [10,5]
print a
def if_num(x,y):
if (x%2 == 0 and y%2 != 0) or (x%2 != 0 and y%2 == 0):
return True
else:
return False
def de_num(z):
'''
x < y
'''
# z = int(z)
print 'de_num runs'
L = []
smid = math.sqrt(z)
if smid == int(smid):
x = int(smid) - 1
y = int(smid) + 1
else:
x = int(smid)
y = int(smid) + 1
# y0 = y
# print x,y
for y0 in range(y,z+1):
# if
# while (x0 * y) < z:
# y += 1
for x0 in range(1,x+1):
if (x0 * y0) < z:
continue
elif (x0 * y0) > z:
break
else:
if if_num(x0,y0):
L.append(y0)
L.append(x0)
if L:
print L
# TODO: 直接用join就调到except部分,因为try出错直接跳到except部分,不会报错
# for i in range(len(L)):
# # if i == 0:
# # s.join(L[i])
# # else:
# # s.join(' '.join(L[i]))
# print 'WAHT'
# # s += L[i]
# s.join(L[i])
# s.join(str(L))
# 方法1
s1 =reduce(lambda x,y:str(x)+str(y),L)
print 'This is result:',s1
# 方法2
L_2 = map(str,L)
s2 = ' '.join(L_2)
print 'The 2 result:',s2
else:
print 'No'
for x in a:
de_num(int(x))
# print '\n'
print '\n'
except:
print ' OK,the script is over\n\n'
程序测试
> ================================ RESTART ================================
Start
TODO: Start A CYCLE: []
1
TDDO2
12
test a: [12]
[12]
de_num runs
[4, 3, 12, 1]
OK,the script is over
Start
TODO: Start A CYCLE: []
2
TDDO2
5
test a: [5]
TDDO2
6
test a: [5, 6]
[5, 6]
de_num runs
No
de_num runs
[3, 2, 6, 1]
OK,the script is over
Start
TODO: Start A CYCLE: []
> ================================ RESTART ================================
Start
TODO: Start A CYCLE: []
1
TDDO2
12
test a: [12]
[12]
de_num runs
[4, 3, 12, 1]
This is result: 43121
The 2 result: 4 3 12 1
TODO: Start A CYCLE: []
2
TDDO2
10
test a: [10]
TDDO2
8
test a: [10, 8]
[10, 8]
de_num runs
[5, 2, 10, 1]
This is result: 52101
The 2 result: 5 2 10 1
de_num runs
[8, 1]
This is result: 81
The 2 result: 8 1
TODO: Start A CYCLE: []
网友评论