Предмет: Информатика, автор: fkid2006

НА С++!! Быстрое возведение в степень
Возводить в степень можно гораздо быстрее, чем за n умножений! Для этого нужно воспользоваться следующими рекуррентными соотношениями:

an=(a2)n/2 при чётном n,

an=a∗an−1 при нечётном n.

Реализуйте алгоритм быстрого возведения в степень. Если вы всё сделаете правильно, то количество умножений будет иметь порядок log2n.

Нельзя использовать встроенную операцию возведения в степень, в силу особенностей тестирующей системы избегайте слова pow в названии функции.

Входные данные

Вводится действительное число a и целое неотрицательное число n.

Выходные данные

Выведите ответ на задачу.

Примеры
Ввод
Вывод
2
7
128
1.00001
100000
2.71827


fkid2006: Можно сюда ещё кинуть халявные 15 баллов: https://znanija.com/task/43920618

Ответы

Автор ответа: dashenkavorobyova
3

Ответ:

#include <iostream>

double s(double a, int er)

{

     if (er == 0)

     {

             return 1;

     }

     if (er % 2 == 0)

     {

             return s(a * a, er / 2);

     }

     return a * s(a, er - 1);

}

int main()

{

     double a;

     int n;

     std::cin >> a >> n;

     std::cout << s(a, n);

     return 0;

}


fkid2006: если хочешь ещё 15 баллов, то кинь сюда: https://znanija.com/task/43920618
fkid2006: А так спасибо, удачи в сириусе
Аноним: Спасибо огромное:)
Похожие вопросы
Предмет: Українська мова, автор: matiyko0808
Предмет: Английский язык, автор: 1TeD
Выберите правильный вариант.
1. The contract ... at the board meeting now.
a) is discussed
b) was discussed
c) is being discussed
d) was being discussed
2. Wait a minute! The document ... by the secretary at the moment.
a) is typed
b) has been typed
c) was typed
d) is being typed
3. The papers ... by the Managing Director yet. He is on business trip.
a) weren't signed
b) aren't signed
c) haven't been signed
d) weren't being signed
4. She ... the Head of the Information Systems Department two months ago.
a) is appointed
b) is being appointed
c) was appointed
d) has been appointed
5. Recently a new generation of TV sets ... in Japan.
a) is being launched
b) has been launched
c) was being launched
d) had been launched
6. At present our company ... .
a) is reorganized
b) is being reorganized
c) has reorganized
d) has been reorganized
7. The mail ... yet.
a) isn't received
b) isn't being received
c) wasn't received
d) hasn't been received
8. From Monday English classes ... in the Training Centre.
a) are held
b) are being held
c) will be held
d) have been held
9. We ... to send the documents by Friday.
a) were asked
b) were being asked
c) have been aksed
d) had been asked
10. At last a new goverment ... after a long govermental crisis.
a) is formed
b) is being formed
c) was formed
d) has been formed
11. My car ... at the moment. Sorry, i can't give you a lift to the station.
a) is being serviced
b) is serviced
c) has been serviced
d) had been serviced
12. All the teaching material ... after the classes start.
a) are provided
b) will be provided
c) had been provided
d) will have been provided