Можно и короче, но так нагляднее: #include <stdlib.h> #include <iostream> using namespace std;
int main() { const int n=6; int a; int b; int c; int arr[n]={-3, -2, -1, 1, 2, 3}; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl;
srand(time(0)); a = rand() % 6; cout << "(" << a << ")" << " a = " << arr[a] << endl; if (a != 0) b = rand() % a; else b=0; cout << " b = " << b << " (" << arr[a-b] << " этаж)" << endl; if ((n-a+b) != 0) c = rand() % (n-a+b); else c=0; cout << " c = " << c << " (" << arr[a-b+c] << " этаж)" << endl; }
Пример: -3 -2 -1 1 2 3 (5) a = 3 b = 4 (-2 этаж) c = 2 (1 этаж)
Если этажей может быть много (до 100), то подробное решение может быть таким: #include <stdlib.h> #include <iostream> using namespace std;
int main() { int n, n1, a, b, c; int a1, a2, a3;
srand(time(0)); n = (rand() % 50 + 1)*2; n1 = n/2; cout << " n = " << n << " (" << -n1 << " ... " << n1 << ")" << endl; a1 = rand() % n; a = a1-n1; if (a <= 0) a--; cout << " a = " << a << endl; b = rand() % a1; a2 = a1-b-n1; if (a2 <= 0) a2--; cout << " b = " << b << " (" << a2 << " этаж)" << endl; if ((n-a1+b) != 0) c = rand() % (n-a1+b); else c=0; a3 = a1-b+c-n1; if (a3 <= 0) a3--; cout << " c = " << c << " (" << a3 << " этаж)" << endl; }
Пример: n = 96 (-48 ... 48) a = -38 b = 9 (-47 этаж) c = 83 (37 этаж)
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cmath>
int main()
{
int
sum(0), count(0),
A, B;
std::cin >> A >> B;
std::vector<int> vec;
std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(vec));
for(auto it = vec.begin(); it != vec.end(); it++)
{
if(A < *it && B > *it && (std::distance(vec.begin(), it) % 4) == 0)
sum += pow(*it, 2);
}
std::cout << "Количество нулей: " << std::count(vec.begin(), vec.end(), 0) << std::endl;
std::cout << "Сумма квадратов чисел: " << sum << std::endl;
}
#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
const int n=6;
int a;
int b;
int c;
int arr[n]={-3, -2, -1, 1, 2, 3};
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
srand(time(0));
a = rand() % 6;
cout << "(" << a << ")" << " a = " << arr[a] << endl;
if (a != 0) b = rand() % a; else b=0;
cout << " b = " << b << " (" << arr[a-b] << " этаж)" << endl;
if ((n-a+b) != 0) c = rand() % (n-a+b); else c=0;
cout << " c = " << c << " (" << arr[a-b+c] << " этаж)" << endl;
}
Пример:
-3 -2 -1 1 2 3
(5) a = 3
b = 4 (-2 этаж)
c = 2 (1 этаж)
Если этажей может быть много (до 100), то подробное решение может быть таким:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
int n, n1, a, b, c;
int a1, a2, a3;
srand(time(0));
n = (rand() % 50 + 1)*2;
n1 = n/2;
cout << " n = " << n << " (" << -n1 << " ... " << n1 << ")" << endl;
a1 = rand() % n;
a = a1-n1;
if (a <= 0) a--;
cout << " a = " << a << endl;
b = rand() % a1;
a2 = a1-b-n1;
if (a2 <= 0) a2--;
cout << " b = " << b << " (" << a2 << " этаж)" << endl;
if ((n-a1+b) != 0) c = rand() % (n-a1+b); else c=0;
a3 = a1-b+c-n1;
if (a3 <= 0) a3--;
cout << " c = " << c << " (" << a3 << " этаж)" << endl;
}
Пример:
n = 96 (-48 ... 48)
a = -38
b = 9 (-47 этаж)
c = 83 (37 этаж)