为什么我的代码会给出运行时错误超时?

时间:2018-02-21 15:00:58

标签: c++ vector square-root

我只想添加 n的sqrt到除数向量,如果它的整数,但每次我尝试代码它都是Haywire。 我的vector.push_back(sqrt(n))函数给出了我感觉到的问题。

发生运行时错误。但当我删除这3行时,它工作正常。

已经尝试了很久!

INPUT - 1 100 8 23 11

我需要帮助!

#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>

#define ll long long int
#define li long int

using namespace std;

bool isPerfectSquare(long long n) {
  long long squareRootN = (long long)round((sqrt(n)));

  if (squareRootN * squareRootN == n) {
    return true;
  } else {
    return false;
  }
}
int main() {
  ios::sync_with_stdio(0);
  ll t;
  cin >> t;
  while (t--) {
    ll n, a, b, c;
    cin >> n;
    cin >> a >> b >> c;

    vector<ll> divisors;
    for (ll i = 1; i < sqrt(n); i++) {
      if (n % i == 0) {
        divisors.push_back(i);
        if ((n / i) != i)
          divisors.push_back(n / i);
      }
    }
    // HEREEEEEEE ISS THE PROBLEMMMM
    ll y = sqrt(n);
    if (isPerfectSquare(n))
      divisors.push_back(y);

    ll z = divisors.size();
    for (ll i = 0; i < z; i++)
      cout << divisors[i] << ' ';

    sort(divisors.begin(), divisors.end());
    cout << '\n';

    ll x = divisors.size();
    for (ll i = 0; i < x; i++)
      cout << divisors[i] << ' ';

    ll endd = divisors.size();

    ll counter = 0;
    for (ll i = 0; i < endd; i++) {
      for (ll j = 0; j <= endd; j++) {
        if (n % (divisors[i] * divisors[j]) == 0 &&
            n / (divisors[i] * divisors[j]) <= c && divisors[i] <= a &&
            divisors[j] <= b) {
          cout << '\n'
               << divisors[i] << ' ' << divisors[j] << ' '
               << n / (divisors[i] * divisors[j]);
          counter++;
        }
      }
    }

    cout << '\n' << counter << '\n';
  }
  return 0;
}

非常感谢!

1 个答案:

答案 0 :(得分:2)

ll endd=diviors.size();
// ...
for (ll j = 0; j <= endd; j++) 
// ...            ^
    divisors[j]

当j = ennd时,您可以访问向量