#include <iostream>
#include <iterator>
#include <numeric>
#include <algorithm>
#include <vector>
unsigned gcd(unsigned a1, unsigned a2) {
while ( a2 != 0) {
unsigned temp = a1 % a2;
a1 = a2;
a2 = temp;
}
return a1;
}
std::vector<unsigned> czytajWartosci() {
using input_stream_t = std::istream_iterator<unsigned>;
std::cout << "Podaj ile liczb chcesz wczytać: ";
// Tu już nie będę sprawdzał czy wczytane dane to liczba...
// sam to zrobisz...
size_t ilosc;
std::cin >> ilosc;
std::vector<unsigned> wartosci(ilosc);
std::copy_n(input_stream_t(std::cin), ilosc, wartosci.begin());
return wartosci;
}
int main() {
auto dane = czytajWartosci();
std::cout << "Dla podanych danych, wspólny dzielnik to: ";
std::cout << std::accumulate(std::next(dane.begin()), dane.end(), *(dane.begin()),
[](unsigned a1, unsigned a2) {
return gcd(a1, a2);
}) << '\n';
}
Tu można uniknąć rekurencji i zastosować std::accumulate dla przejścia po kontenerze wczytywanych liczb.