C/C++

Хелпаните плиз, надо код перестроить на N вещестные числа и для вычесления гометрической прогрессии.

Буду очень благодарна.
Задана последовательность N вещественных чисел. Определите, можно ли так перестроить ее элементы, чтобы они образовали геометрическую прогрессию.

код прилагаю (написан на C++):
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincon.h>
#pragma hdrstop

#pragma argsused
using namespace std;

int main()
{ SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int N;
cout << "Введите количество чисел: ";
cin >> N;
int counter = 0;
float sum = 0;

while (N)
{
int t;
cout << "Введите следующее число: ";
cin >> t;
if (t%7 == 0) {sum += t; counter++;}
N--;
}

cout << "Среднее арифметическое чисел, кратных 7: ";
cout << sum/counter << endl;
getch();
return 0;
}
Aman Sainov
Aman Sainov
79
Спасибо за код :)) Во-первых, он для совсем другой задачи, во-вторых, написан на устаревшем диалекте C++, который давно не поддерживается :))

#include "iostream"
#include "algorithm"
#include "cmath"
using namespace std;
int main(){
size_t n; cout<<"n: "; cin>>n; double *a=new double[n]; bool b=true;
cout<< n<<" numbers: "; for(size_t i=0;i< n;i++)cin>>a[i];
sort(a,a+n,[](double i,double j){return abs(i)<abs(j);});
float k=a[1]/a[0]; for(size_t i=1;i< n;i++)if(float(a[i]/a[i-1])!=k)b=false;
delete []a; cout<< boolalpha<< b<< endl;}
Роман Хрящёв
Роман Хрящёв
58 610
Лучший ответ
// C++20
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
bool is_geometric_progression(const vector<double>& box) {
const auto length = box.size();
if (length < 2) return false;
const auto q = fabs(box[1] / box[0]);
const auto eps = 1e-12;
for (auto i = 2U; i < length; ++i) {
const auto x = fabs(box[i] / box[i - 1]);
if (fabs(x - q) > eps) return false;
}
return true;
}
bool task(const vector<double>& box) {
auto tmp = box;
sort(tmp.begin(), tmp.end());
if (is_geometric_progression(tmp)) return true;
tmp = box;
auto cmp = [](double a, double b) {
return fabs(a) < fabs(b);
};
sort(tmp.begin(), tmp.end(), cmp);
if (fabs(tmp[0]) != fabs(tmp[1])) {
if (is_geometric_progression(tmp)) return true;
}
auto negative = [](double x) {
return x < 0;
};
auto neg = count_if(tmp.begin(), tmp.end(), negative);
auto positive = [](double x) {
return x > 0;
};
auto pos = count_if(tmp.begin(), tmp.end(), positive);
return abs(neg - pos) < 2;
}
int main() {
cout << "N >>> ";
size_t n;
cin >> n;
cout << "Elements >>> ";
vector<double> box(n);
for (auto& x : box) cin >> x;
puts(task(box) ? "Yes" : "No");
system("pause > nul");
}

Похожие вопросы