#29118: java function


Imcyj123 (test.java)

School : 朝陽科技大學
ID : 160420
IP address : [36.232.232.41]
Last Login :
2024-05-29 01:51:43
c184. 盈虧互補 -- 板橋高中教學題 | From: [36.238.221.141] | Post Date : 2022-01-30 15:13

真因數 => 從1..n/2

建議先寫 d010

---

程式碼如下:

 

import java.util.Scanner;
class c184 {
static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
boolean bool = true;
while (bool) {
int n = in.nextInt();
if (n == 0) {
bool = false;
break;
}
if (num(n) == n) {
System.out.println("=" + n);
} else if (num(num(n)) == n) {
System.out.println(num(n));
} else {
System.out.println(0);
}
}
}
static int num(int n) {
int res = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) res += i;
}
return res;
}
}
 
ZeroJudge Forum