본문 바로가기
알고리즘

Do it! 자료구조와 함께 배우는 알고리즘 입문 (자바편) - 반복 (Q7~9)

by Love of fate 2021. 9. 2.
728x90
반응형

* Test7 : n이 7이면 '1+2+3+4+5+6+7=28'로 출력하는 프로그램을 작성하세요.


* Test8 : 1부터 10까지의 합은 (1+10)*5와 같은 방법으로 구할 수 있습니다. 가우스의 덧셉이라는 방법을 이용하여 1부

  터 n까지의 정수 합을 구하는 프로그램을 작성하세요.

 

   * 가우스 덧셈 

 

* Test9 : 정수 a,b를 포함하여 그 사이의 모든 정수의 합을 구하여 반환하는 아래 메서드를 작성하세요.

static int sumof(int a, int b)

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package algorithm;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Scanner;
 
public class TestQ7 {
 
    public static void main(String[] args) {
        
        //Test 7
        Scanner sc = new Scanner(System.in);
        System.out.println("--Test 7------------------------------------------");
        System.out.println("1부터 n까지의 합을 구합니다.");
        System.out.print("n의 값: ");
        
        int n = sc.nextInt();
        int sum = 0;
        List<Integer> arrayList = new ArrayList<Integer>();
        
        for(int i=1; i <= n; i++) {
            sum += i;
            arrayList.add(i-1, i);
        }
        
        System.out.println("1부터 " + n + "까지의 합은 " + sum + "입니다.");
        
        System.out.println("\n--Test 8------------------------------------------");
        
        System.out.println("가우스의 덧셈으로 1부터 n까지의 합을 구합니다.");
        
        int lastInt = 0;
        int gausSum = 0;
        
        if(arrayList.size() % 2 == 1) {
            lastInt = arrayList.get(arrayList.size() -1);
            
            arrayList.remove(arrayList.size()-1);
            gausSum = gausSum(arrayList);
            
            gausSum += lastInt;
                        
        }else {
            gausSum = gausSum(arrayList);
        }
        
        System.out.println("1부터 " + n + "까지의 합은 " + gausSum + "입니다.");
        
        System.out.println("\n--Test 9------------------------------------------");
        
        System.out.println("정수 a, b를 포함하여 그 사이의 모든 정수의 합을 구하여 반환하는 아래 메서드를 작성하세요");
        System.out.println("static int sumof(int a, int b)");
        System.out.print("a : ");
        int a = sc.nextInt();
        System.out.print("b : ");
        int b = sc.nextInt();
        
        int result = sumof(a, b);
        System.out.println("정수 a, b를 포함한 그 사이 모든 정수의 합은 :" + result);
        
    }
    
    public static int gausSum(List<Integer> array) {
        int result = 0;
        
        for(int i = 0; i < array.size()/2; i++) {
            int first = array.get(i);
            int last = array.get(array.size() - (i+1));
            
            result += (first + last);
        }
        
        return result;
    }
    
    
    public static int sumof(int a, int b) {
        
        int result = 0;
        
        for(int i = a; i <= b; i++) {
            result += i;
        }
        
        return result;
    }
}
 
cs

 

※ 결과 

728x90
반응형