Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Филипповых Матвей
- Group: СБС-901
- вариант: 4.2
- Description: Каждая строка в двумерной матрице представляет собой двоичное число(элементы строки могут принимать только два значения - нуль или единица).
- Найти номера строк, модуль разности чисел которых - максимален.
- */
- #include <cstdio>
- #include <clocale>
- #include <cstdlib>
- #include <ctime>
- #include <locale>
- #include<iomanip>
- /*void outputting(int lines, int column, int the_binary_number_system, double A[][])
- {
- for (int i = 0; i < lines; i++)
- {
- printf("\nA[%d]=", i + 1);
- for (int j = 0; j < column; j++)
- {
- the_binary_number_system = A[i + 1][j + 1];
- printf("%d ", the_binary_number_system);
- }
- }
- }*/
- void Outputting_Matrix(double* Element_of_array[], const int stroki, const int stolb) {
- for (int j = 0; j < stolb; j++) {
- if (j == 0) {
- printf(" %d\t", j + 1);
- }
- else
- printf("%d\t", j + 1);
- }
- for (int i = 0; i < stroki; i++) {
- printf("\n");
- printf("%d|", i + 1);
- for (int j = 0; j < stolb; j++) {
- printf("%lf", Element_of_array[i][j]);
- }
- }
- printf("\n");
- }
- int random()
- {
- double rnd = double(rand()) / RAND_MAX;
- int rnd2;
- if (rnd > 0.5) {
- rnd2 = 1;
- }
- else {
- rnd2 = 0;
- }
- printf("%d", rnd2);
- return rnd2;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- const size_t MAX_SIZE = 101;
- double A[MAX_SIZE][MAX_SIZE];
- srand(time(NULL));
- /*спрашиваем сколько строк и стобцов*/
- printf("Введите количесвто строк \n");
- int lines;
- scanf_s("%d", &lines);
- while (lines < 0 || lines>MAX_SIZE)
- {
- printf("нельзя вводить отрицательные числа и числа больше 1000 строк\n");
- printf("Введите количествово строк");
- scanf_s("%d", &lines);
- }
- printf("Введите количесвто столбцов \n");
- int column;
- scanf_s("%d", &column);
- while (column < 0 || column>MAX_SIZE)
- {
- printf("нельзя вводить отрицательные числа и числа больше 1000 для столбцов\n");
- printf("Введите количествово столбцов");
- scanf_s("%d", &column);
- }
- /*вводим числа в матрицу*/
- printf("хотите сами ввести числа в матрицу? если да напишите\"1\" иначе любую другую цифру");
- int answer = 0;
- scanf_s("%d", &answer);
- int the_binary_number_system;
- if (answer == 1)
- {
- for (int i = 0; i < lines; i++)
- {
- for (int j = 0; j < column; j++)
- {
- printf("\nA[%d][%d]=", i + 1, j + 1);
- scanf_s("%d", &the_binary_number_system);
- while (the_binary_number_system != 0 && the_binary_number_system != 1)
- {
- printf("нельзя вводить не двоичные числа \n");
- scanf_s("%d", &the_binary_number_system);
- }
- A[i + 1][j + 1] = the_binary_number_system;
- }
- }
- /*вывод на консоль*/
- double* matr[MAX_SIZE];
- for (int i = 0; i < lines; i++)
- matr[i] = A[i];
- Outputting_Matrix(matr, lines, column);
- /* for (int i = 0; i < lines; i++)
- {
- printf("\nA[%d]=", i + 1);
- for (int j = 0; j < column; j++)
- {
- the_binary_number_system = A[i + 1][j + 1];
- printf("%d ", the_binary_number_system);
- }
- }
- */
- }
- else
- {
- for (int i = 0; i < lines; i++)
- {
- for (int j = 0; j < column; j++)
- {
- A[i + 1][j + 1] = random();
- }
- }
- /*вывод на консоль*/
- for (int i = 0; i < lines; i++)
- {
- printf("\nA[%d]=", i + 1);
- for (int j = 0; j < column; j++)
- {
- the_binary_number_system = A[i + 1][j + 1];
- printf("%d ", the_binary_number_system);
- }
- }
- }
- /*начинается алгоритм счета*/
- int B[MAX_SIZE];
- /*перевод из двоичного в десятиричное*/
- for (int i = 0; i < lines; i++)
- {
- int the_number_of_the_decimal_system = 0;
- for (int j = 0; j < column; j++)
- {
- the_binary_number_system = A[i + 1][column - j];
- the_number_of_the_decimal_system = the_binary_number_system * pow(2, j) + the_number_of_the_decimal_system;
- }
- B[i + 1] = the_number_of_the_decimal_system;
- }
- /*нахождение максимального модуля разности*/
- int max = 0;
- for (int i = 0; i < lines-1; i++)
- {
- for (int j=1 ; j < lines; j++) {
- int first_number = B[i + 1];
- int second_number = B[j+1];
- if (max < abs(first_number - second_number)) {
- max = abs(first_number - second_number);
- }
- }
- }
- /*выписывание ответа*/
- for (int i = 0; i < lines-1; i++)
- {
- for (int j=1; j < lines; j++) {
- int first_number = B[i + 1];
- int second_number = B[j+1];
- if (max <= abs(first_number - second_number)) {
- printf("\nA[%d] и A[%d]", i + 1, j+1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement