ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 2997번: 네번째 수
    정수론(Number theory) 2018. 6. 13. 19:49
    반응형

    https://www.acmicpc.net/problem/2997



    1. 문제 요약

    상근이는 등차수열을 이루는 정수 4개를 골랐다. 이것은 상근이가 고른 수 4개를 정렬했을 때, 인접한 쌍의 차이가 일정하다는 것을 의미한다. 그 다음 이렇게 고른 숫자 4개를 노래로 만들어서 외우고 다닌다. 어느날, 상근이는 자신이 고른 4개 숫자 중 1개를 까먹었다. 상근이가 고른 네 개의 숫자 중 세 개가 주어졌을 때, 네번째 숫자를 구하는 프로그램을 작성하시오.


    2. 알고리즘

    수열을 입력 받고 정렬 한다. 

    각각의 수에서 차이 값을 구한다.


    차이가 갔다면 3번째 수열에 덧셈을 해주고

    차이가 다르다면 2 번째 수열에 뻴셈을 해준다.


    3. 코드

    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
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <functional>         // greater 사용 위해 필요  
    #include <string>
    #include <map>
    #include <math.h>
    using namespace std;
     
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0);
     
        int n=3;
        vector<int> arr(n);
     
        for(int i=0; i<n; i++)
            cin >> arr[i];
     
        sort(arr.begin(), arr.end());
     
        int b = arr[2- arr[1];
        int a = arr[1- arr[0];
     
        if (b == a)
            cout << arr[2+ b << endl;
        else if (b > a)
            cout << arr[2- a << endl;
        else
            cout << arr[1- b << endl;
        return 0;
    }
    cs

    반응형

    '정수론(Number theory)' 카테고리의 다른 글

    백준 10610번: 30  (0) 2018.06.29
    백준 5086번: 배수와 약수  (0) 2018.06.21
    백준 2581번 : 소수  (0) 2018.06.20
    백준 1978번 : 소수 찾기  (0) 2018.06.20
    프로그래머스 Level1 > 정수 제곱근 판별  (0) 2018.06.14

    댓글

Designed by Tistory.