Advertisement
Petro_zzz

lesson12_322

Sep 1st, 2023
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. //#include <Windows3.h>
  5.  
  6. using namespace std;
  7.  
  8. void calc_average() {
  9.     int b;
  10.     cout << "Ввоедите оценки для вычисления вседнего\n";
  11.     cout << "Для окончания введите 0\n";
  12.  
  13.     int sum = 0, n = 0;
  14.     //b = 1;
  15.     cin >> b;
  16.     while (b > 0 && b <= 5) {
  17.         sum += b;
  18.         n++;
  19.         cin >> b;
  20.     }
  21.  
  22.     cout << "Средний балл: "
  23.         << (double)sum / n
  24.         << endl;
  25. }
  26.  
  27. double power(double x, int n) {
  28.     /*double x = ;
  29.     int n = ;*/
  30.    
  31.     if (n == 0)
  32.         return 1;
  33.        
  34.     double y = 1;
  35.     while (n < 0) {
  36.         y /= x;
  37.         n++;
  38.     }
  39.     while (n > 0) {
  40.         y *= x;
  41.         n--;
  42.     }
  43.     //return pow(x, n);
  44.     return y;
  45. }
  46.  
  47. double power2(double x, int n) {
  48.     double y = 1;
  49.     if (n < 0) {
  50.         n = -n;
  51.         x = 1 / x;  // x = x / 2  --> x /= 2
  52.     }
  53.     while (n > 0) {
  54.         y *= x;
  55.         n--;
  56.     }
  57.     //return pow(x, n);
  58.     return y;
  59. }
  60.  
  61. void table_mult() {
  62.     cout << "x\\y\t";
  63.     int x = 2, y = 1;
  64.  
  65.     while (y < 10) {
  66.         while (x < 10) {
  67.             cout << x * y << '\t';
  68.             x++;
  69.         }
  70.         x = 1;
  71.         cout << endl;
  72.         y++;
  73.     }
  74. }
  75.  
  76. int get_rand(int a, int b) {
  77.     return rand() % (b - a + 1) + a;
  78. }
  79.  
  80. int get_rand2(int a, int b) {
  81.     int y = rand();
  82.     while (y < a || y > b) {
  83.         y = rand();
  84.     }
  85.     return y;
  86. }
  87.  
  88. int one_humanstep(int n) {
  89.     int number;
  90.     switch (n)
  91.     {
  92.     case 1: cout << "Первая"; break;
  93.     case 2: cout << "Вторая"; break;
  94.     case 3: cout << "Третья"; break;
  95.     default: cout << n << "-ая"; break;
  96.     }
  97.     cout << " попытка. Введите число :";
  98.     cin >> number;
  99.     if (number < 0 || number > 500)
  100.         number = 0;
  101.     return number;
  102. }
  103.  
  104.  
  105. bool run_game() {
  106.     /*
  107.            "УГАДАЙ ЧИСЛО"
  108.  
  109.         Ваша задача угадать число за
  110.         наименьшее число попыток. Если
  111.         Вы сдаётесь нажмите 0.
  112.  
  113.         Начнём игру? 1 - да, 0 - нет.
  114.         1
  115.         Я загадал число от 0 до 500. Угадывай.
  116.  
  117.         Первая попытка. Введите число: 250.
  118.  
  119.         250 больше числа, которое я загадал.
  120.  
  121.         Вторая попытка. Введите число: 120.
  122.         120 меньше числа, которое я загадал.
  123.  
  124.         Третья попытка. Введите число: 134.
  125.         Поздравляю, Вы угадали.
  126.     */
  127.  
  128.     cout << "      \"УГАДАЙ ЧИСЛО\"    " << endl;
  129.     cout << R"(
  130. Ваша задача угадать число за
  131. наименьшее число попыток.
  132. Если Вы сдаётесь нажмите 0.
  133. )" << endl;
  134.     cout << "Начнём игру? 1 - да, 0 - нет." << endl;
  135.     char is_start;
  136.     int ai_number, number;
  137.     int step = 0;
  138.     cin >> is_start;
  139.     /*
  140.     while (is_start != '0' && is_start != '1') {
  141.         cin >> is_start;
  142.     }
  143.     */
  144.     bool result;
  145.     if (is_start == '1') {
  146.         cout << "Я загадал число от 0 до 500. Угадывай." << endl;
  147.         ai_number = get_rand(1, 500);
  148.         //ai_number = 55;
  149.         long long time0 = time(NULL);
  150.         while (true) {
  151.             step++;
  152.             number = one_humanstep(step);
  153.  
  154.             if (number <= 0 || number == ai_number) {
  155.                 break;
  156.             }
  157.             cout << number;
  158.             //cout << ((number > ai_number)? " больше" : " меньше");
  159.             (number > ai_number) ? cout << " больше" : cout << " меньше";      
  160.             cout << " числа, которое я загадал." << endl;
  161.         }      
  162.         if (number == ai_number) {
  163.             cout << "Выйграли. ";
  164.             result = true;
  165.         }
  166.         else {
  167.             cout << "Проиграли. ";
  168.             result = false;
  169.         }
  170.         cout << "Число попыток " << step
  171.             << " за " << time(NULL) - time0
  172.             << " секунд(ы)." << endl;
  173.     }
  174.     return result;
  175. }
  176.  
  177. void test_random() {
  178.     cout << "time: " << time(NULL) << endl << endl;
  179.     int n = 0;
  180.     while (n < 50) {
  181.         //int x = rand() % 500 + 1; /// [0, 499] --> [1, 500]
  182.         int x = get_rand(-5, 5);
  183.         cout << x << endl;
  184.         n++;
  185.     }
  186. }
  187.  
  188. void start_game() {
  189.     int is_repeat = 1;
  190.     char ch = 'y';
  191.     int game = 0, wins = 0;
  192.     while (ch == 'y' || ch == 'Y') {
  193.         game++;
  194.         wins += run_game();
  195.         cout << "Хотите ещё играть? 'y' - да, 'n' - нет. ";
  196.         cin >> ch;
  197.         while (ch != 'y' && ch != 'Y' &&
  198.             ch != 'n' && ch != 'N') {
  199.             cout << "Не корректный ввод ";
  200.             cin >> ch;
  201.         }
  202.     }
  203.     cout << "В/И: " << wins << "/" << game << endl;
  204. }
  205.  
  206. void test_getch() {
  207.     int k = 0;
  208.     while (k < 50) {
  209.         k++;
  210.         cout << get_rand(1, 6) << endl;
  211.         _getch(); // #include <conio.h>    
  212.     }
  213. }
  214.  
  215. void test_break() {
  216.     int k = 0;
  217.     while (k < 10) {
  218.         k++;       
  219.         if (k % 3 == 0) {
  220.             cout << '#' << endl;
  221.             continue;
  222.         }
  223.         else {
  224.             cout << k << endl;
  225.             if (k == 8)
  226.                 break;
  227.         }
  228.     }
  229. }
  230.  
  231. int get_sumdel(int n) {
  232.     int k = 0, res = 0;
  233.     while (k <= n / 2) {
  234.         k++;
  235.         if (n % k)
  236.             continue;
  237.         res += k;
  238.     }
  239.     return res + n;
  240. }
  241.  
  242. int main() {
  243.     setlocale(LC_ALL, "ru");
  244.     srand(time(NULL));
  245.  
  246.     /*one_humanstep(1);
  247.     one_humanstep(2);
  248.     one_humanstep(3);
  249.     one_humanstep(400);
  250.     */
  251.     //table_mult();
  252.     //calc_average();
  253.     //cout << power(2, -4) << endl;
  254.     /*cout << 2 * power(val, 4) << endl;
  255.     double y = power(3.0, 4) * sin(0.1);
  256.     cout << y << endl;*/
  257.     //test_random();
  258.     //test_break();
  259.  
  260.     int x = get_sumdel(13);
  261.     cout << x << endl;
  262.    
  263.     system("pause");
  264. }
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement