1 / 4
Feb 2023

Próbuje rozwiązać zadanie Czy umiesz potęgować i w programie wyniki wychodzą poprawne ale spoj uznaje to jako złą odpowiedź, czy ktoś mógłby mi powiedzieć gdzie tu jest problem?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int *results = NULL;
int looptimes;
int number;
int exponent;
int result;
cin>>looptimes;
results = new int[looptimes];

for(int i = 0; i < looptimes; i++){
cin>>number>>exponent;

result = pow(number, exponent);
result = result % 10;
results[i] = result;
}

for(int i = 0; i < looptimes; i++){
	cout << results[i] << endl;
}

return 0;
}
  • created

    Feb '23
  • last reply

    Feb '23
  • 3

    replies

  • 362

    views

  • 3

    users

  • 2

    likes

  • 1

    link

pozmieniałem kod i nie pokazuje błędu ale zajmuje za dużo czasu, ma ktoś jakiś pomysł na optymalizacje?

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int *results = NULL;
int looptimes;
int number;
int exponent;
int result = 1;
cin>>looptimes;
results = new int[looptimes];

for(int i = 0; i < looptimes; i++){
cin>>number>>exponent;
for (int j = 0; j < exponent; j++){
result = (result * number) % 10;
}
results[i] = result;
result = 1;
}

for(int i = 0; i < looptimes; i++){
	cout << results[i] << endl;
}

delete results;

return 0;
}

Czytania ze zrozumieniem uczą na początku podstawówki, ale w twoim przypadku najwyraźniej nieskutecznie. Przeczytaj jeszcze raz post korkirwa, może więcej niż raz i dostosuj się