Advertisement
kalabukdima

using namespaces

Jan 7th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <cassert>
  2. #include <algorithm>
  3.  
  4. namespace correct {
  5. struct Foo {};
  6.  
  7. int global_variable = 0;
  8.  
  9. int64_t solve(int n) {
  10.     int64_t result = 0;
  11.     for (int i = std::min(0, n); i <= std::max(0, n); ++i) {
  12.         result += i;
  13.     }
  14.     return result;
  15. }
  16. } //namespace correct
  17.  
  18.  
  19. namespace fast {
  20. struct Foo {};
  21.  
  22. int global_variable = 0;
  23.  
  24. int64_t solve(int n) {
  25.     return 1ll * n * (n + 1) / 2;
  26. }
  27. } //namespace fast
  28.  
  29.  
  30. int main() {
  31.     for (int n : {0, 1, 2, 1'000'000}) { // C++11: 1000000
  32.         assert(fast::solve(n) == correct::solve(n));
  33.     }
  34.     assert(fast::solve(-1) == correct::solve(-1)); // Oops!
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement