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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

通過二維數(shù)組存儲(chǔ)一個(gè)數(shù)字的質(zhì)因數(shù)

通過二維數(shù)組存儲(chǔ)一個(gè)數(shù)字的質(zhì)因數(shù)

www說 2023-10-13 14:48:33
我正在編寫一種方法來存儲(chǔ)數(shù)字的素因數(shù)。我被要求使用二維數(shù)組來存儲(chǔ)其素因數(shù)和因數(shù)的數(shù)量。public static int[][] getMatrix (long x){        int[][] matrix =new int[10][2];        int count;        for (int i = 2, j = 0; i <=x / 2; i++) {           count=0;           while (x % i == 0) {               x = x/i;               count++;           }           matrix[j][0] = i;           matrix[j][1] = count;           j++;       }       return matrix;   }但此代碼僅將數(shù)據(jù)存儲(chǔ)到數(shù)組的第一行。有人可以幫我糾正它或提供其他想法嗎?如果我使用下面的代碼來輸出結(jié)果。for(int row=0;row<b_matrix.length;row++)        {            for(int column=0;column<2;column++)            {                System.out.print(b_matrix[row][column]+" ");            }        }x=9 我得到這個(gè):2 0 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x=6 我得到這個(gè):2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 如:6matrix[0][0]=2  matrix[0][1]=1matrix[1][0]=3  matrix[1][1]=1 //can't store如:9matrix[0][0]=2   matrix[0][1]=0//only output the next row when this equals to 0 matrix[1][0]=3   matrix[1][1]=2
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

你的邏輯是正確的,除了在 for 循環(huán)中i應(yīng)該直到x而不是x/2如下,


for (int i = 2, j = 0; i <= x; i++)

輸出getMatrix(60):


2   2   

3   1   

4   0   

5   1   

0   0   

0   0   

0   0   

0   0   

0   0   

0   0   


查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 1 回答
  • 0 關(guān)注
  • 118 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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