C/C++

Нужен текст программы, только чтоб без ошибки запустился в Dev-c-++ Нужно как можно быстрее

Используя одну функцию, подсчитать количество элементов меньшие заданной целой величины t и количество элементов, кратных t. Применить данную функцию для целочисленных массивов m1[20] и m2[25]
#include <iostream>
#include <random>
#include <iomanip>
#include <ctime>
using namespace std;
pair<unsigned, unsigned> fn(int t, int* box, size_t n) {
pair<unsigned, unsigned> res{ 0, 0 };
for (auto i = 0U; i < n; ++i) {
if (box[i] < t) ++res.first;
if (0 == box[i] % t) ++res.second;
}
return res;
}
int main() {
system("chcp 1251 > nul");
srand(unsigned(time(nullptr)));
unsigned t;

int m1[20];
for (auto& m : m1) m = 1 + rand() % 100;
for (auto m : m1) cout << setw(4) << m;
puts("");
cout << "t: ";
cin >> t;
auto a = fn(t, m1, sizeof(m1) / sizeof(m1[0]));
cout
<< "Меньше " << t << ": " << a.first << '\n'
<< "Кратные " << t << ": " << a.second << "\n\n";

int m2[25];
for (auto& m : m2) m = 1 + rand() % 100;
for (auto m : m2) cout << setw(4) << m;
puts("");
cout << "t: ";
cin >> t;
auto b = fn(t, m2, sizeof(m2) / sizeof(m2[0]));
cout
<< "Меньше " << t << ": " << b.first << '\n'
<< "Кратные " << t << ": " << b.second << "\n";
system("pause > nul");
}
Денис Гордиенко
Денис Гордиенко
68 082
Лучший ответ
#include "iostream"
#include "utility"
#include "algorithm"
#include "iomanip"
#include "ctime"
#include "cstdlib"
using namespace std;
typedef pair< int, int> tp;
tp f(const int t, int *a, int n){
tp p; p.first=count_if(a,a+n,[t](int i){return i< t;});
p.second=count_if(a,a+n,[t](int i){return !(i%t);});
return p;}
int main(){
const int n1=20, n2=25; int t, m1[n1], m2[n2]; tp p; srand(time(NULL));
cout<<"t: "; cin>>t; cout<< endl;
for(int &i:m1)cout<< setw(4)<<(i=rand()%199-99); cout<< endl;
p=f(t,m1,n1); cout<<"<t: "<< p.first<<", mod t=0: "<< p.second<< endl;
cout<< endl;
for(int &i:m2)cout<< setw(4)<<(i=rand()%199-99); cout<< endl;
p=f(t,m2,n2); cout<<"<t: "<< p.first<<", mod t=0: "<< p.second<< endl;}
Ваня Байда
Ваня Байда
78 776
#include<stdio.h>
void check(int arr[],int size,int t,int* small,int* divisible)
{
int i=0;
for(i=0;i<size;i++)
{
if(arr[i]<t) *small=*small+1;
if(arr[i]%t==0)
*divisible=*divisible+1;
}
printf("\n array of %d digits:",size);
printf("\n smaller digits: %d",*small);
printf("\n divisible digits: %d\n\n",*divisible);
}

int main()
{
int m1[20]={1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,6,7,8,9,10};
int m2[25]={5,6,7,8,9,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4};
int small=0,divisible=0,t=0;
int* psmall=&small;
int* pdiv=&divisible;
printf("\n t = ");
scanf("%d",&t);
check(m1,20,t,psmall,pdiv);
check(m2,25,t,psmall,pdiv);
return 0;
}
Влад Дан
Влад Дан
37 945