Witam,
Na początku miałem problem z przekroczeniem czasu w tym zadaniu: https://pl.spoj.com/problems/SKO/
Jednak poradziłem sobie z tym, ale teraz mam błędną odpowiedź, nie mam pojęcia czemu, może ktoś z was wie.
#include <iostream>
#include <math.h>
#include <vector>
#include <limits>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n;
cin >> n;
bool whileTrue = true;
vector<int> tab;
for (int j = 0; j < n; j++) {
int y;
cin >> y;
tab.push_back(y);
if ((y > 6 || y < 1) && whileTrue == true) {
cout << "-1" << endl;
whileTrue = false;
}
}
double h = 1;
int k = 1;
while (whileTrue) {
double wynik = 0;
vector<int>::const_iterator it;
for (it = tab.begin(); it != tab.end();it++) {
wynik += pow(*it, k);
}
h = (1.0000 / k);
wynik /= n;
wynik = pow(wynik, h);
if (wynik >= numeric_limits<double>::infinity() || h == 0) {
cout << "-1" << endl;
break;
}
else if (wynik >= 4) {
cout << k << endl;
break;
}
k++;
}
}
return 0;
}