Блог пользователя Destopia

Автор Destopia, история, 23 месяца назад, По-английски

Consider following code:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int sum = 0;
  for (auto &it : a) {
    cin >> it;
    sum += it;
  }
  cout << sum << "\n";
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

Input like (or anything greater than INT_MAX into k)

5 1234567891564
1 2 3 4 5

makes the program print


0 0 0 0 0 0

What actually happens? We don't use the value of $$$k$$$ at all.

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
23 месяца назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

It seems that the attempt to read a long long into an int variable breaks cin and it doesn't read anything else.