fork(13) download
  1. #include <cmath>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <limits.h>
  5.  
  6. int idiv_ceil ( int numerator, int denominator )
  7. {
  8. return numerator / denominator + (((numerator < 0) ^ (denominator > 0)) && (numerator%denominator));
  9. }
  10.  
  11. void test( int n, int d )
  12. {
  13. int using_float = (int) std::ceil( (double)n / d );
  14. int using_fn = idiv_ceil(n, d);
  15. std::cout << std::setw(15) << n
  16. << std::setw( 5) << d
  17. << std::setw(15) << using_float
  18. << std::setw(15) << using_fn
  19. << std::setw( 8) << ((using_float == using_fn)? "ok" : "ERROR") << std::endl;
  20. }
  21.  
  22. int main(void)
  23. {
  24. test(INT_MAX, 1);
  25. test(-INT_MAX, 1);
  26. test(INT_MIN, 1);
  27. for( int n = -5; n < 5; n++ ) {
  28. for ( int d = 1; d < 5; d++ ) {
  29. test(n, d);
  30. test(n, -d);
  31. }
  32. }
  33. }
  34.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
     2147483647    1     2147483647     2147483647      ok
    -2147483647    1    -2147483647    -2147483647      ok
    -2147483648    1    -2147483648    -2147483648      ok
             -5    1             -5             -5      ok
             -5   -1              5              5      ok
             -5    2             -2             -2      ok
             -5   -2              3              3      ok
             -5    3             -1             -1      ok
             -5   -3              2              2      ok
             -5    4             -1             -1      ok
             -5   -4              2              2      ok
             -4    1             -4             -4      ok
             -4   -1              4              4      ok
             -4    2             -2             -2      ok
             -4   -2              2              2      ok
             -4    3             -1             -1      ok
             -4   -3              2              2      ok
             -4    4             -1             -1      ok
             -4   -4              1              1      ok
             -3    1             -3             -3      ok
             -3   -1              3              3      ok
             -3    2             -1             -1      ok
             -3   -2              2              2      ok
             -3    3             -1             -1      ok
             -3   -3              1              1      ok
             -3    4              0              0      ok
             -3   -4              1              1      ok
             -2    1             -2             -2      ok
             -2   -1              2              2      ok
             -2    2             -1             -1      ok
             -2   -2              1              1      ok
             -2    3              0              0      ok
             -2   -3              1              1      ok
             -2    4              0              0      ok
             -2   -4              1              1      ok
             -1    1             -1             -1      ok
             -1   -1              1              1      ok
             -1    2              0              0      ok
             -1   -2              1              1      ok
             -1    3              0              0      ok
             -1   -3              1              1      ok
             -1    4              0              0      ok
             -1   -4              1              1      ok
              0    1              0              0      ok
              0   -1              0              0      ok
              0    2              0              0      ok
              0   -2              0              0      ok
              0    3              0              0      ok
              0   -3              0              0      ok
              0    4              0              0      ok
              0   -4              0              0      ok
              1    1              1              1      ok
              1   -1             -1             -1      ok
              1    2              1              1      ok
              1   -2              0              0      ok
              1    3              1              1      ok
              1   -3              0              0      ok
              1    4              1              1      ok
              1   -4              0              0      ok
              2    1              2              2      ok
              2   -1             -2             -2      ok
              2    2              1              1      ok
              2   -2             -1             -1      ok
              2    3              1              1      ok
              2   -3              0              0      ok
              2    4              1              1      ok
              2   -4              0              0      ok
              3    1              3              3      ok
              3   -1             -3             -3      ok
              3    2              2              2      ok
              3   -2             -1             -1      ok
              3    3              1              1      ok
              3   -3             -1             -1      ok
              3    4              1              1      ok
              3   -4              0              0      ok
              4    1              4              4      ok
              4   -1             -4             -4      ok
              4    2              2              2      ok
              4   -2             -2             -2      ok
              4    3              2              2      ok
              4   -3             -1             -1      ok
              4    4              1              1      ok
              4   -4             -1             -1      ok