// PascalABC.NET 3.1, сборка 1218 от 12.04.2016 begin var n:=ReadInteger('N='); var m:=ReadInteger('M='); var a:array of array of integer; SetLength(a,N); for var i:=0 to n-1 do begin a[i]:=ArrRandom(m,10,99); a[i].Println end; Println('Различных элементов',a.SelectMany(x->x).ToHashSet.Count) end.
В C++ #include <iostream> #include <iomanip> #include <ctime> int main() { using namespace std;
const int N = 5; const int M = 6;
int A[N][M];
//Как-нибудь заполняем массив srand(time(0)); for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) A[i][j] = rand() % (N * M) + 1; for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) cout << setw(4) << A[i][j]; cout << endl; }
//Подсчитываем сумму всех элементов массива int sum = 0;
for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) sum += A[i][j];
//Вычитаем из полученной суммы повторяющиеся элементы for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) { bool flag = false; for (int i1 = 0; i1 < N; ++i1) { for (int j1 = 0; j1 < M; ++j1) if (!(i == i1 && j == j1)) if (A[i][j] == A[i1][j1]) { sum -= A[i][j]; flag = true; break; } if (flag) break; } }
cout << "Sum of different: " << sum << endl;
return 0; }
0,0(0 оценок)
Полный доступ
Позволит учиться лучше и быстрее. Неограниченный доступ к базе и ответам от экспертов и ai-bota
Оформи подписку