2011-02-06 58 views
2

这是我工作的问题:http://www.codechef.com/problems/HS08TEST/CodeChef#3,简单,也能收到错误的答案

这是我的解决方案:

#include <stdio.h> 

int main (void) { 
     int withdraw_i; 
     float balance_i; 

     scanf("%d %f", &withdraw_i, &balance_i); 

     if(withdraw_i % 5 == 0) { 
       if(withdraw_i <= balance_i) { 
         float result = balance_i - withdraw_i - 0.5; 
         printf("%.2f", result); 
       } 
       else { 
         printf("%.2f", balance_i); 
       } 
     } 
     else { 
       printf("%.2f", balance_i); 
     } 
     return 0; 
} 

(在C) 它完全适用于页面上显示的测试,但是,当我提交它时,我仍然不断收到错误“错误的答案”,发生了什么事?

+1

不要为钱使用浮点数! – Svante 2011-02-06 16:39:51

回答

1

这是因为您没有正确实施此测试的条件。尝试一下“300 300”。

1

应该

if(withdraw_i+0.5 <= balance_i) 
+0

非常感谢:) – 2011-02-06 16:20:27