第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過比較索引找到集合中的最大長度

通過比較索引找到集合中的最大長度

冉冉說 2022-05-21 20:22:11
我正在嘗試用java編寫一個程序,通過比較找到整個集合中任何一對坐標(biāo)之間的最大長度。我想我的問題不在于找到最大長度,而在于創(chuàng)建所有可能的配對。我真的不明白我應(yīng)該為給定方程中的 x 和 y 坐標(biāo)設(shè)置什么。我如何從每對中獲取坐標(biāo)并進行比較?我已經(jīng)閱讀了重復(fù)的解決方案,但我對編程還很陌生,我無法理解它們。這是我到目前為止與此問題相關(guān)的代碼部分:public static void main (String[] args) {    int[][] countries = {{-34,-7},{76,-23},{-5,-20},{-56,64},{12,56}};double maxDistance = 0;    for(int i = 0; i < countries.length-1; ++i) {      for(int j = i; j < countries.length; ++j) {        double distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));我將不勝感激任何幫助,甚至是朝著正確方向的推動。我對如何定義 x/y 變量感到困惑,因為我只是從國家數(shù)組中提取坐標(biāo),所以我認為它們已經(jīng)定義了?我在想會有一種方法來比較等式中的索引或其他東西。
查看完整描述

2 回答

?
江戶川亂折騰

TA貢獻1851條經(jīng)驗 獲得超5個贊

我對您的代碼進行了一些更改


int[][] countries = { { -34, -7 }, { 76, -23 }, { -5, -20 }, { -56, 64 }, { 12, 56 } };


double maxDistance = 0;

for (int i = 0; i < countries.length - 1; ++i) {

    int x1 = countries[i][0];

    int y1 = countries[i][1];

    for (int j = i; j < countries.length; ++j) { // first j will be i

        int x2 = countries[j][0];

        int y2 = countries[j][1];

        double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

        maxDistance = maxDistance < distance ? distance : maxDistance;

    }

}

System.out.println(maxDistance);

輸出


158.09174551506476


注意我如何循環(huán)i和j,以及我如何定義x1, x2, y1, y2


查看完整回答
反對 回復(fù) 2022-05-21
?
慕標(biāo)5832272

TA貢獻1966條經(jīng)驗 獲得超4個贊

在 i 之后開始您的 j 因為您已經(jīng)將先前的索引與數(shù)組的其余部分進行了比較。而且,您是否定義了 x1、x2、y1、y2 變量?


這應(yīng)該有助于:


int[][] countries = {{-34,-7},{76,-23},{-5,-20},{-56,64},{12,56}};


double maxDistance = 0;

    for(int i = 0; i < countries.length-1; ++i) {

      for(int j = i+1; j < countries.length; ++j) { //do not create zero distance or duplicate pairings

        //get your x1, x2, y1, y2 from countries[i] and countries[j]

        double distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

        //compare with maxDistance

        //assign maxDistance if you find it greater than previous one and record the coordinates which produced this distance

    }

}

如果有不清楚的地方或者我錯過了什么,請發(fā)表評論;)


查看完整回答
反對 回復(fù) 2022-05-21
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號