§ 6. Выкарыстанне асноўных алгарытмічных канструкцый для рашэння задач

Практыкаванні

    

1. Напішыце праграму для вылічэння першых n элементаў паслядоўнасці Фібаначы.

1. Для якога максімальнага значэння n праграма выдае карэктны адказ?
2. Змяніце праграму так, каб яна выводзіла значэнне ліку Фібаначы па ўведзеным нумары.
3. Замяніце ў праграме тып int на тып long long. Якое максімальнае значэнне n можна ўвесці зараз?

2. Напішыце праграму, якая будзе выводзіць на экран элементы паслядоўнасці трыбаначы — першыя элементы паслядоўнасці: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149… . Кожны элемент, пачынаючы з чацвёртага, роўны суме трох папярэдніх: an = an – 1an – 2an – 3.

1. Па зададзеным n вывесці элемент паслядоўнасці.
2. Для зададзенага x вывесці элементы паслядоўнасці, меншыя x.

3. Напішыце праграму, якая знойдзе першы адмоўны элемент паслядоўнасці sin(tgn)n = 1, 2, ... і яго нумар.

4. Выканайце заданні для прыкладу 6.15.

1. Замяніце ў рашэнні задачы цыкл  for на цыкл while або dowhile.
2. Ваня вырашыў скараціць колькасць радкоў у праграме і запісаў цыкл наступным чынам:

    for (int n = 1; n <= m; n++){

      double a = n * n * n / (sqrt(* n * n)- n + 1);

      s += a;

    }

Чаму для  m = 2000 Ваня атрымаў у адказе  s = nan?

5. Напішыце праграму, якая знойдзе суму першых m элементаў паслядоўнасці. Лік m уводзіцца. Элементы паслядоўнасці задаюцца формулай.

begin mathsize 16px style 1. space a subscript n space equals space 1 over n cubed.

2. space a subscript n space equals space fraction numerator 2 cos n over denominator left parenthesis n space plus space 1 right parenthesis squared end fraction.

3. space a subscript n space equals space fraction numerator square root of n cubed end root over denominator n squared space plus space n space plus space 3 end fraction.

4. space a subscript n space equals space fraction numerator fourth root of n space plus space 3 end root over denominator n cubed space plus space 4 n end fraction.
end style

6. Напісаць праграму для вылічэння сумы, якая мае сваімі складаемымі элементы паслядоўнасці a subscript n. Вылічэнні выконваць да таго часу, пакуль не знойдзецца складаемае, для якога правільная няроўнасць open vertical bar a subscript n close vertical bar space less than space e p s. Значэнне eps уводзіцца (0 < eps < 1 ).

begin mathsize 16px style 1. space a subscript n space equals space fraction numerator 2 to the power of n space plus space n over denominator 5 to the power of n end fraction.

2. space a subscript n space equals space fraction numerator 3 to the power of n over denominator n factorial space plus space 2 end fraction.

3. space a subscript n space equals left parenthesis – 1 right parenthesis to the power of n space fraction numerator 3 to the power of n space plus space 1 over denominator n factorial space plus space 5 end fraction.

4. space a subscript n space equals space 2 over 3 to the power of n space plus space fraction numerator 2 over denominator n factorial end fraction.
end style

7. Напішыце праграму, якая знойдзе здабытак з n сумножнікаў наступнага выгляду. Значэнне n уводзіцца.


1. space open parentheses 1 space plus space 1 over 1 squared close parentheses open parentheses 1 space plus space 1 over 2 squared close parentheses open parentheses 1 space plus space 1 over 3 squared close parentheses... open parentheses 1 space plus space 1 over n squared close parentheses.

2. space a open parentheses a space plus 1 close parentheses... open parentheses a space plus space n space – 1 close parentheses. space Лік space а space ўводзіцца.

3. space open parentheses space 1 space minus space 1 over 1 squared close parentheses open parentheses 1 space minus space 1 over 2 cubed close parentheses open parentheses 1 space minus space 1 over 2 to the power of 4 close parentheses... open parentheses 1 space minus space 1 over 2 to the power of straight n space plus 1 end exponent close parentheses.

4. space open parentheses x space plus space 2 x squared space plus x cubed close parentheses left parenthesis straight x squared space plus space 2 straight x cubed space plus straight x to the power of 4 right parenthesis left parenthesis straight x cubed space plus space 2 straight x to the power of 4 space plus straight x to the power of 5 right parenthesis... left parenthesis straight x to the power of n space plus space 2 straight x to the power of n italic space plus space 1 end exponent space plus straight x to the power of n space plus space 2 end exponent right parenthesis.

Лік space х space space уводзіцца.

8. Выканайце заданне для прыкладу 6.17.

    1. Замяніце ў рашэнні задачы цыкл for на цыкл while або  dowhile.
    2.  Атрымайце табліцу значэнняў функцыі на адрэзку [–3; 3]. У якасці ўмовы ў цыкле можна выкарыстоўваць наступнае: x < = 3;
    3. Дабаўце ў праграму вывад меж вакол табліцы:

9. Напішыце праграму, якая пабудуе табліцы значэнняў для наступных функцый.


1. space y space equals space x squared space – space 5 x space – space 3 comma space x element of space open square brackets – 3 semicolon space 3 close square brackets comma space уводзіцца space значэнне space кроку space h.

2. space y space equals cos space x space – space x squared comma space space x element of space open square brackets – straight pi semicolon space straight pi close square brackets comma space space уводзіцца space колькасць space пунктаў.

3. space y space equals 2 space plus space fraction numerator 3 x minus 7 over denominator x squared space plus space 1 end fraction comma space space x element of space open square brackets a italic comma italic space b close square brackets to the power of left square bracket 1 right square bracket end exponent comma space space уводзяцца space значэнні space space а comma space b space straight і space колькасць space пунктаў.

4. space y space equals 2 space plus space 1 over x sin x comma space space x element of space open square brackets a italic comma italic space b close square brackets comma space у водзяцца space значэнні space space a comma space b space straight і space колькасць space пунктаў. space
Неабходна space ўлічыць space вобласць space вызначэння space функцыі.

10. Праграму з прыкладу 6.18 змянілі. Сфармулюйце задачу, якая рашаецца з  дапамогай дадзенай праграмы.

#include<iostream>

 

using namespace std;

 

int main()

{

  int i, n;

  cout << "n = ";

  cin >> n;

  cout << "i = ";

  cin >> i;

  int k = 0;

  while (> 0)

  {

    int z = n % 10//бягучая лічба

    k++;

    if (== i){

      cout << "v razrjade " << i;

      cout << " stoit zifra "<< z << endl;

    }

  n /= 10; //памяншэнне ліку ў 10 разоў

  }

  if (> k){

    cout << "v chisle "<<k<<" cifr, ";

    cout << "v razrjade " << i << " net cifr " << endl;

  }

  else

  cout<<"v chisle "<<k<<" cifr";

  return 0;

}

11. Дадзены натуральны лік n. Напішыце праграму, якая вызначыць, якіх лічбаў у ліку больш, цотных ці няцотных.

12. Дадзены натуральны лік n. Напішыце праграму, якая выведзе нумары разрадаў, у якіх стаяць лічбы, кратныя 3, ці паведамленне, што такіх лічбаў няма.

13. Напішыце праграму, якая выведзе на экран лічбу, што стаіць на сярэдняй пазіцыі ліку, калі лік мае няцотную колькасць лічбаў, ці 2 сярэднія для ліку з цотнай колькасцю лічбаў.

14. Напішыце праграму, якая пасля кожнай лічбы 1 у ліку ўставіць яшчэ адну адзінку. Напрыклад, из 51214  → 5112114.

15. Выканайце заданне для прыкладу 6.19.

1. Для сітуацыі, калі цукар заканчваецца, выведзіце значэнне часу, калі гэта адбылося.
2. Змяніце праграму так, каб задавалася пачатковая і канчатковая колькасць цукру, а разлічваўся час, неабходны для такога змянення канцэнтрацыі. 
3. Змяніце праграму так, каб па ўведзеным часе і колькасці цукру ў пачатку і ў канцы працэсу разлічваўся пачатковы аб’ём раствору.



[1] Падказка: begin mathsize 14px style h space equals space fraction numerator b space – space a over denominator k space – space 1 end fraction. end style