Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <Windows.h>
  5.  
  6. int main()
  7. {
  8.     SetConsoleCP(1251);
  9.     SetConsoleOutputCP(1251);
  10.     const int nmax = 100;
  11.     int a[nmax];//dannye
  12.     int b[nmax];//bin schetchik
  13.     int c[nmax];
  14.     memset(c, 0, sizeof(c));
  15.     int count, i, sum, polsum;
  16.     printf("Введите кол-во элементов:\n");
  17.     scanf_s("%d", &count);
  18.     printf("Введите вес элементов:\n");
  19.     sum = 0;
  20.     for (int i = 0; i < count; i++)
  21.     {
  22.         scanf_s("%d", &a[i]);
  23.         sum = sum + a[i];
  24.     }
  25.     polsum = sum / 2;
  26.     memset(b, 0, sizeof(b));
  27.     b[0] = 1;
  28.     int min = nmax;
  29.     int summas = 0;
  30.     int str2=0;
  31.     int razn = nmax;
  32.     printf("Первый массив\n");
  33.     do
  34.     {
  35.         summas = 0;
  36.         for (i = 0; i < count; i++)
  37.             if (b[i] == 1) {
  38.                 summas += a[i];
  39.                 str2++;
  40.             }
  41.         razn = abs(summas - polsum);
  42.         if (razn < min) {
  43.             memcpy(c,b,sizeof(b));
  44.             min = razn;
  45.         }
  46.         str2 = 0;
  47.         i = 0;
  48.         while (b[i] == 1)
  49.             b[i++] = 0;
  50.         b[i] = 1;
  51.     } while (i < count);
  52.     for (int i = 0; i < count; i++)
  53.             if (c[i] == 1)
  54.                 printf("%d\n", a[i]);
  55.        
  56.         printf("Второй массив\n");
  57.         for (int i = 0; i < count; i++)
  58.             if (c[i] == 0)
  59.                 printf("%d\n", a[i]);
  60.        
  61.     system("pause");
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement