1. 程式人生 > >計算機17-3,4作業F

計算機17-3,4作業F

+= ava while asn tle amp scrip 作業 content

F.complete number problem with formatted output

Description
同題目E
Input
N
Output

complete numbers within 2~N

Sample Input

10

100

Sample Output

6=1+2+3

6=1+2+3

28=1+2+4+7+14

 1 import java.util.*;
 2 public class Main{
 3     public static void main(String[] args){
 4
Scanner in = new Scanner(System.in); 5 while(in.hasNext()){ 6 int num = in.nextInt(); 7 for(int i=6;i<=num;i++){ 8 int sum = 0; 9 int[] arr = new int[100]; 10 int k = 0; 11 for(int j=1;j<i;j++)
12 if(i%j==0){ 13 sum += j; 14 arr[k++] = j; 15 } 16 if(sum==i){ 17 System.out.print(i+"="); 18 for(int l=0;l<k;l++){ 19 System.out.print(arr[l]);
20 if(l!=k-1) 21 System.out.print("+"); 22 } 23 System.out.println(); 24 } 25 } 26 } 27 } 28 }

計算機17-3,4作業F