egogoboy

"Числа Смита"

Jun 28th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>;
  4. using namespace std;
  5.  
  6. int sum_num(int x) {
  7.  
  8.     int answ = 0;;
  9.  
  10.     while (x > 0) {
  11.         answ += x % 10;
  12.         x /= 10;
  13.     }
  14.  
  15.     return answ;
  16.  
  17. }
  18.  
  19. bool elementary(int x) {
  20.  
  21.     for (int i = 2; i < x; i++) {
  22.         if (x % i == 0) {
  23.             return false;
  24.         }
  25.     }
  26.  
  27.     return true;
  28.  
  29. }
  30.  
  31. int sum_mn(int x) {
  32.  
  33.     int answ = 0;
  34.     while (x != 1) {
  35.         for (int i = 2; i <= x; i++) {
  36.             if (x % i == 0 && elementary(i)) {
  37.                 if (answ == 0 && i == x) {
  38.                     return -1;
  39.                 }
  40.                 else {
  41.                     answ += sum_num(i);
  42.                     x /= i;
  43.                     break;
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     return answ;
  49. }
  50.  
  51. int main() {
  52.  
  53.     std::ifstream fin("input.txt");
  54.     std::ofstream fout("output.txt");
  55.  
  56.     vector<int> a;
  57.  
  58.     int x;
  59.     while (fin >> x) {
  60.         a.push_back(x);
  61.     }
  62.  
  63.     for (int i = 0; i < a.size(); i++) {
  64.  
  65.         if (sum_num(a[i]) == sum_mn(a[i])) {
  66.             fout << '1';
  67.         }
  68.         else {
  69.             fout << '0';
  70.         }
  71.  
  72.     }
  73.  
  74.     return 0;
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment