2012-01-11 94 views
-1

我是python的新手。这里是我的wiered Python代码,它适用于所有输入,除了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 
+2

代码应该做什么? – Kimvais 2012-01-11 14:53:23

+0

请修复您的缩进。我们无法分辨'while'循环结束的位置。 – 2012-01-11 14:53:52

回答

7

你自己已经解开了这个谜团(种):

if int(r)%2==0 and r!=2 and r!=0 and c!=0: 

r是一个字符串。因此r将始终为!=2,因为"2" != 2始终为真。所有其他比较也是如此。

我猜你第一次得到TypeErrorif r%2==0,所以你改变了那一点的程序(也在所有其他地方,你实际上正在计算的值),但忽略了这个洞察应用到该计划的其他部分。因此,首先将所有输入转换为int s,然后开始应用程序逻辑。

+0

检查输出r = 100和c = 0,它给出25而不是50 – anil 2012-01-11 15:00:28

+0

你纠正了你的代码吗? – 2012-01-11 15:01:28

+0

好吧,先生,非常感谢你在开始时将r和c转换为int。 – anil 2012-01-11 15:03:37