hdu 2040 親和數 (java)

來源:互聯網
上載者:User

標籤:java   acm   

問題:

開始給for迴圈的J設定了小於根號a,導致約數沒有全部得到。


親和數 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26302    Accepted Submission(s): 15814


Problem Description
古希臘數學家畢達哥拉斯在自然數研究中發現,220的所有真約數(即不是自身的約數)之和為:

1+2+4+5+10+11+20+22+44+55+110=284。

而284的所有真約數為1、2、4、71、 142,加起來恰好為220。人們對這樣的數感到很驚奇,並稱之為親和數。一般地講,如果兩個數中任何一個數都是另一個數的真約數之和,則這兩個數就是親和數。

你的任務就編寫一個程式,判斷給定的兩個數是否是親和數 
Input輸入資料第一行包含一個數M,接下有M行,每行一個執行個體,包含兩個整數A,B; 其中 0 <= A,B <= 600000 ; 
Output對於每個測試執行個體,如果A和B是親和數的話輸出YES,否則輸出NO。 
Sample Input
2220 284100 200
 
Sample Output
YESNO


代碼:

import java.util.*;public class Main{public static void main(String[] args) {Scanner cin=new Scanner(System.in);int n=cin.nextInt();for(int i=0;i<n;i++){int a=cin.nextInt();int b=cin.nextInt();int k=0,t=0;for(int j=1;j<a;j++)if(a%j==0)k=k+j;for(int j=1;j<b;j++)if(b%j==0)t=t+j;if(k==b&&t==a)System.out.println("YES");elseSystem.out.println("NO");}}}


hdu 2040 親和數 (java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.