RAINGM
@RAINGM

Как перевести c++ задачу на javascript, пытался перевести, но матрица не генерировалась?

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <conio.h>
#include <locale.h>
using namespace std;

int main()
{
	setlocale(LC_ALL, "rus");
	srand(time(NULL));
	int  imx, jmx;
	double k;
	cout << "Координаты x: ";

	const int n = 5;
	int x[n];
	for (int i = 0; i < n; i++)
	{
		x[i] = 1 + rand() % (30);
	}

	for (int i = 0; i < n; i++)
		cout << x[i] << "    ";
	cout << "     " << endl;
	cout << "Координаты y: ";
	int y[n];
	for (int i = 0; i < n; i++)
	{
		y[i] = 1 + rand() % (30);
	}

	for (int i = 0; i < n; i++)
		cout << y[i] << "    ";
	k = sqrt(pow(x[1] - x[2], 2) + pow(y[1] - y[2], 2));
	imx = 0; jmx = 1;
	for (int i = 1; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
		{
			if (sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2)) > k)
			{
				k = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2));
				imx = i + 1;
				jmx = j + 1;

			}
		}
	cout << "     " << endl;
	cout << "Максимальное расстояние между точками (x" << imx << ",y" << imx << ")";
	cout << " и (x" << jmx << ",y" << jmx << ")";
	cout << " = ";
	cout << k;
	return 0;
}


Вот так перевёл на js, но неправильно
<script>
                function main()
                {
                    var random_number_x = Math.floor(Math.random() * 101);
                    var random_number_y = Math.floor(Math.random() * 101);

                    var  imx, jmx;
                    var k;
                    console.log("Координаты x: ")

                    const n = 5;
                    let x = new Array(n);
                    for (var i = 0; i < n; i++)
                    {
                        x[i] = 1 + random_number_x % (30);
                    }

                    for (var i = 0; i < n; i++)
                        console.log(x[i]);

                    console.log("Координаты y: ")
                    let y = new Array(n);
                    for (var i = 0; i < n; i++)
                    {
                        y[i] = 1 + random_number_y % (30);
                    }

                    for (var i = 0; i < n; i++)
                        console.log(y[i], "    ");
                    k = Math.sqrt(Math.pow(x[1] - x[2], 2) + Math.pow(y[1] - y[2], 2));
                    imx = 0; jmx = 1;
                    for (var i = 1; i < n - 1; i++)
                        for (var j = i + 1; j < n; j++)
                        {
                            if (Math.sqrt(Math.pow(x[i] - x[j], 2) + Math.pow(y[i] - y[j], 2)) > k)
                            {
                                k = Math.sqrt(Math.pow(x[i] - x[j], 2) + Math.pow(y[i] - y[j], 2));
                                imx = i + 1;
                                jmx = j + 1;
                            }
                        }
                        
                    console.log("Максимальное расстояние между точками (x" + imx + ",y" + imx + ")");
                    console.log(" и (x" + jmx + ",y" + jmx + ")");
                    console.log(" = ");
                    console.log(k);
                    return 0;

                    document.getElementById('result').innerHTML = "Максимальное расстояние между точками (x", imx, ",y", imx, ")";
                }

                document.getElementById('count').addEventListener('click', main);
            </script>
  • Вопрос задан
  • 84 просмотра
Пригласить эксперта
Ответы на вопрос 1
Programiker
@Programiker
Програмикер
Duocode.com
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы