Informatică, întrebare adresată de MaxMic, 8 ani în urmă

Promblema in c++, dau si coronita cine rezolva corect.

Anexe:

Răspunsuri la întrebare

Răspuns de Porecla0987
0

#include <iostream>

#include <vector>

#include <fstream>

using namespace std;

struct Line

{

int nrlin, a, b, c, gcd;

 

 

Line(int lineNumber, int a, int b, int c)

{

 this->nrlin = lineNumber;

 this->a = a;

 this->b = b;

 this->c = c;

 int divisor = 1, greatestCommonDivisor = 1;

 while (divisor <= a && divisor <= b && divisor <= c)

 {

  if (a % divisor == 0 && b % divisor == 0 && c % divisor == 0)

   greatestCommonDivisor = divisor;

  ++divisor;

 }

 this->gcd = greatestCommonDivisor;

}

 

 

Line &operator=(Line other)

{

 nrlin = other.nrlin;

 a = other.a;

 b = other.b;

 c = other.c;

 gcd = other.gcd;

}

 

 

static void swap(Line &self, Line &other)

{

 Line temp = self;

 self = other;

 other = temp;

}

};

void bubbleSort(std::vector<Line> &lines)

{

for (int i = 0; i < lines.size() - 1; ++i)

{

 for (int j = 0; j < lines.size() - i - 1; ++j)

 {

  if (lines[j].gcd < lines[j + 1].gcd)

  {

   Line::swap(lines[j], lines[j + 1]);

  }

 }

}

}

int main()

{

ifstream fin("divizor.in");

ofstream fout("divizor.out");

int nrlin, a, b, c;

std::vector<Line> lines;

while (fin >> nrlin >> a >> b >> c)

{

 Line line(nrlin, a, b, c);

 if (line.gcd != 1)

  lines.emplace_back(line);

}

bubbleSort(lines);

for (Line line : lines)

{

 fout << line.nrlin << " "

      << line.a << " "

      << line.b << " "

      << line.c << " "

      << line.gcd << endl;

}

}

Alte întrebări interesante