acm 2040 java做法

來源:互聯網
上載者:User

標籤:ext   list   sample   lis   rgs   input   古希臘   problem   bsp   

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 Input2220 284100 200 

 

Sample OutputYESNO
    public class Main {        public static void main(String[] args) {                Scanner in= new Scanner(System.in);                                      int n=in.nextInt();                    for(int ii=0;ii<n;ii++){                        int a=in.nextInt();                        int b=in.nextInt();                        int t=0;                                                List<Integer> li = new ArrayList<Integer>();                        for(int i=1;i<=(a/2);i++){                            if(a%i==0){                                li.add(i);                            }                        }                        for(int i:li){                            t=t+i;                        }                        if(t==b){                            li.clear();                            t=0;                            for(int i=1;i<=(b/2);i++){                                if(b%i==0){                                    li.add(i);                                }                            }                            for(int i:li){                                t=t+i;                            }                            if(t==a){                                System.out.println("YES");                            }                        }else{                            System.out.println("NO");                        }                                            }                }      }

 

acm 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.