Skip to main content
Code Review

Return to Revisions

2 of 6
added 113 characters in body
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
  1. Don't force a class where a function is perfectly fine.

  2. The namespace std is not designed for wholesale importation, see "Why is "using namespace std" considered bad practice? " for more detail.

  3. Identifiers starting with an underscore followed by an uppercase letter are reserved.

  4. Input is generally unreliable. But you don't test for error at all.

  5. Always recomputing the current result from first principles, instead of just updating it with the newest addition, is a waste of time.

  6. Flushing is expensive, so avoid std::endl and explicitly request it with std::flush where unavoidable.

  7. You should probably follow the requested output-format...

  8. You know that int in unsigned int is implicit?

  9. In C++ and C99+, return 0; is implicit for main().

  10. I wonder why you sometimes surround binary operators with space, and sometimes don't.

  11. Having over-long lines is very cumbersome to read. Admittedly, you need not make a hard cut at 79 nowadays.

  12. As it is an interview question, one should verify that one should really use a Set, meaning that repeated elements have no effect, aside from causing an additional output.

  13. As it is an interview question, one should verify that, contrary to the example, the inputs might not be sorted.

#include <iostream>
#include <set>
#include <cstdlib>
int main() {
 unsigned n, m, x;
 std::cin >> n >> m;
 if (!std::cin || !n) {
 std::cerr << "Bad Input!\n";
 return EXIT_FAILURE;
 }
 std::set nums{ 0u, n + 1};
 auto r = 0ULL;
 while (--m) {
 std::cin >> x;
 if (!std::cin || x < 1 || x > n) {
 std::cerr << "Bad Input!\n";
 return EXIT_FAILURE;
 }
 auto [iter, created] = nums.insert(x);
 if (created)
 r += 2 * x;
 std::cout << r << '\n';
 }
}
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65
default

AltStyle によって変換されたページ (->オリジナル) /