我是python的新手。这是我的wiered python代码,除了
之外,它适用于所有输入当c = 0且r!= 0时。
我有多个测试用例(tc),r和c作为输入,根据条件提供所需的输出。
问题---输入r = 4& c = 0,输出应为2,但输出为1。我为每一个r!= 0& C = 0。
代码:
tc=int(input())
while tc:
r,c=raw_input().split()
if int(r)%2==0 and r!=2 and r!=0 and c!=0:
r=int(r)/2
elif r!=2 and r!=0 and c!=0:
r=int(r)/2+1
elif r==0 or r ==2:
r=1
if r!=0:
if int(c)!=0:
print(int(r)*int(c))
else :
if int(r)%2==0 :
print(int(r)/2)
else:
r=int(r)/2+1
print(r)
else :
print(c);
tc=tc-1
示例输入和输出
4 //tc
10 10 //r=10 c= 10
50 //fine
3 3 //r=3 c=3
6 //fine
4 0 //r=4 c=0
1 //Should be 2 accoring to code
5 0 //r=5 c=0
2 //Output should be 3 accoring to the code
答案 0 :(得分:7)
你自己解决了这个谜(有点):
if int(r)%2==0 and r!=2 and r!=0 and c!=0:
r
是一个字符串。因此r
始终为!=2
,因为"2" != 2
始终为真。所有其他比较也是如此。
我猜你首先得到TypeError
if r%2==0
,所以你改变了程序的那一点(也在你实际用值计算的所有其他地方)但是忽略了将这种洞察力应用于该计划的其他部分。
首先将所有输入转换为int
,然后开始应用程序逻辑。