Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include "stdio.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10. typedef std::vector<double> Vec;
  11. typedef std::vector<std::vector<double> > Mat;
  12.  
  13. int egyesek_szamolasa(Vec x) {
  14.     int egyesek = 0;
  15.     for(int c = 0; c < x.size(); c++) {
  16.         if(x[c] == 1) {
  17.             egyesek++;
  18.         }
  19.     }
  20.     return egyesek;
  21. }
  22. void printVec(Vec a) {
  23.     for(int i = 0; i < a.size(); i++) {
  24.         printf("%0.lf ", a[i]);
  25.     }
  26. }
  27. void printMat(Mat A) {
  28.     for(int i = 0; i < A.size(); i++) {
  29.         for(int j = 0; j < A[0].size(); j++) {
  30.             printf("%0.lf ", A[i][j]);
  31.         }
  32.         printf("\n");
  33.     }
  34. }
  35. Vec next_generated_vector(int n) {
  36.     Vec x;
  37.     for (int i = 23; i >= 0; i--) {
  38.         int k = n >> i;
  39.         if (k & 1){
  40.             x.push_back(1);
  41.         } else {
  42.             x.push_back(0);
  43.         }
  44.     }
  45.     return x;
  46. }
  47. void szamolas(Mat A) {
  48. //legenerlunk egy sorvektor
  49.     int meddig = pow(2,24);
  50.     Vec x;
  51.     int kodszo = 0, egyesek=0;
  52.     int min_egyesek=24;
  53.     for(int k = 1; k < meddig; k++) {
  54.         x = next_generated_vector(k);
  55.         int sum = 0;
  56.         for(int i = 0; i < 12; i++) {
  57.             for(int j = 0; j < 24; j++) {
  58.                 sum += A[i][j] * x[j];
  59.             }
  60.             if (sum%2==1) {
  61.                 //printf(" A %d. sor osszege(mod2-ben) %d tehat kilepunk\n", i, sum);
  62.                 x.clear();
  63.                 sum = 0;
  64.                 break;
  65.             }
  66.             else if(sum%2==0 && i==11) {
  67.                 egyesek = egyesek_szamolasa(x);
  68.                 printVec(x);
  69.                 printf(" :%d\n", egyesek);
  70.                 if(egyesek <= min_egyesek) {
  71.                     min_egyesek = egyesek;
  72.                 }
  73.  
  74.                 egyesek = 0;
  75.                 kodszo++;
  76.                 x.clear();
  77.             }
  78.         }
  79.     }
  80.     printf("%d. kodszo \n", kodszo);
  81.     printf("Legkevesebb 1-est tartalmazo kodszo: %d\n", min_egyesek);
  82. }
  83.  
  84. Mat transzponalj(Mat A) {
  85.     Mat B;
  86.     Vec x;
  87.     float a;
  88.     for(int i = 0; i < A.size(); i++) {
  89.         for(int j = 0; j < A[0].size(); j++) {
  90.             a = A[j][i];
  91.             x.push_back(a);
  92.             a = 0;
  93.         }
  94.         B.push_back(x);
  95.     }
  96.     return B;
  97. }
  98.  
  99. int main()
  100. {
  101.     Mat A;
  102.     double q;
  103.     Vec x;
  104.     //Beolvasas
  105.     for(int i = 0; i < 12; i++){
  106.         for(int j = 0; j < 24; j++) {
  107.             scanf("%lf", &q);
  108.             x.push_back(q);
  109.         }
  110.         A.push_back(x);
  111.         x.clear();
  112.     }
  113.  
  114.     Mat B;
  115.     //Levalasszuk amit transzponalni akarunk
  116.     for(int i = 0; i < 12; i++) {
  117.         q = 0;
  118.         for(int j = 12; j < 24; j++) {
  119.             double q = A[i][j];
  120.             x.push_back(q);
  121.         }
  122.         B.push_back(x);
  123.         x.clear();
  124.     }
  125.     transzponalj(B);
  126.  
  127.     //Levalasszuk az A matrix elejet
  128.     Mat C;
  129.     for(int i = 0; i < 12; i++) {
  130.         q = 0;
  131.         for(int j = 0; j < 12; j++) {
  132.             double q = A[i][j];
  133.             x.push_back(q);
  134.         }
  135.         C.push_back(x);
  136.         x.clear();
  137.     }
  138.  
  139.     //Fuzzok ossze a C Ês a B Matrixot
  140.     Mat D;
  141.     for(int i = 0; i < 12; i++) {
  142.         for(int j = 0; j < 24; j++) {
  143.             if(j<12) {
  144.                 double q = B[i][j];
  145.                 x.push_back(q);
  146.             }
  147.             else {
  148.                 double q = C[i][j-12];
  149.                 x.push_back(q);
  150.             }
  151.         }
  152.         D.push_back(x);
  153.         x.clear();
  154.     }
  155.     printf("\n");
  156.     printMat(D);
  157.  
  158.     szamolas(D);
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement