MaksNew

Untitled

Nov 1st, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int input(int& m) {
  8.     bool IsNotCorrect;
  9.     cout << "Введите m" << endl;
  10.     do {
  11.         IsNotCorrect = false;
  12.         try {
  13.             string value;
  14.             cin >> value;
  15.             m = stoi(value);
  16.         }
  17.         catch (...) {
  18.             cout << "Введите натуральное число" << endl;
  19.             IsNotCorrect = true;
  20.         }
  21.         if (m <= 0 && !IsNotCorrect) {
  22.             cout << "Введите натуральное положительное число" << endl;
  23.             IsNotCorrect = true;
  24.         }
  25.     } while (IsNotCorrect);
  26.     return m;
  27. }
  28.  
  29.  
  30. bool getBool(int m, int** sos, int count, bool *boof) {
  31.     int x;
  32.     int y;
  33.     int z;
  34.     int x3;
  35.     int y3;
  36.     int z3;
  37.     int j;
  38.     int i = 0;
  39.     int msqr;
  40.     msqr = round(sqrt(m / 3));
  41.     (*boof) = true;
  42.     for (int i = 0; i <= count; i++)
  43.     {
  44.         sos[i] = new int[3];
  45.     }
  46.     for (x = 1; x <= msqr; x++) {
  47.         x3 = x * x * x;
  48.         if (x3 < m) {
  49.             for (y = 1; y <= msqr; y++) {
  50.                 y3 = y * y * y;
  51.                 if (x3 + y3 < m) {
  52.                     for (z = 1; z <= msqr; z++) {
  53.                         z3 = z * z * z;
  54.                         j = x3 + y3 + z3;
  55.                         if (j == m) {
  56.                             sos[i][0] = x;
  57.                             sos[i][1] = y;
  58.                             sos[i][2] = z;
  59.                             i++;
  60.                             count++;
  61.                             (*boof) = false;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68.     return count;
  69. }
  70.  
  71.  
  72.  
  73. void output(bool boof, int** sos, int count) {
  74.     if (!boof) {
  75.         cout << "Числа, удовлетворяющие условию:" << endl;
  76.         for (int i = 0; i < count; i++) {
  77.             for (int j = 0; j < 3; j++)
  78.             {
  79.                 cout << sos[i][j] << "\t";
  80.             }
  81.             cout << endl;
  82.         }
  83.     }
  84.     else {
  85.         cout << "Не удалось подобрать числа X, Y, Z для данного числа M" << endl;
  86.     }
  87. }
  88.  
  89. int main()
  90. {
  91.     int m;
  92.     bool boof;
  93.     int count = 0;
  94.     int** sos = new int* [3];
  95.     setlocale(LC_ALL, "Russian");
  96.     m = input(m);
  97.     count = getBool(m, sos, count, &boof);
  98.     output(boof, sos, count);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment