구현(Implementation)

백준 10539번: 수빈이와 수열

cepiloth 2018. 6. 13. 18:23
반응형

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


1. 문제 요약

정수 수열 A를 쓴다. 그리고 그 아래에 정수 수열 A의 해당 항까지의 평균값을 그 항으로 하는 정수 수열 B를 쓴다. 

예를 들어, 수열 A가 1, 3, 2, 6, 8이라면, 수열 B는 1/1, (1+3)/2, (1+3+2)/3, (1+3+2+6)/4, (1+3+2+6+8)/5, 즉, 1, 2, 2, 3, 4가 된다. 

수열 B가 주어질 때, 수빈이의 규칙에 따른 수열 A는 뭘까?


수열 B를 보고 수열 A 를 유추 하는 문제 


2. 알고리즘

수열 B 가 3, 2, 3, 5 순으로 주어 진다면


수열의 첫번 째는 3 이다.

수열의 두번 째는 2 = (3 + x) / 2 로 정리 할수 있다.

4 = 3 + x

x + 3 = 4

x = 4 - 3 

x = 1 로 계산 할 수 있다.

두번 째 수열은 1이 된다.


세번째 수열은

3 = (3 + 1 + x) /3 

9 = 3 + 1 + x

3 + 1 + x = 9

x = 9 - 3 - 1

x = 5 가 된다.


마지막 수열 까지 반복 한다.


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
33
34
35
36
37
38
39
40
#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; cin >> n;
    vector<int> arr(n);
 
    for(int i=0; i<n; i++) {
        cin >> arr[i];
    }
 
    vector<int> brr(n);
    brr[0= arr[0];
 
    for(int i=1; i<n; i++) {
        int cand = arr[i];
 
        int data = 0;
        for(int j=0; j<i; j++) {
            data += brr[j];
        }
 
        int sol = cand *(i+1- data;
        brr[i] = sol;
    }
 
    for(int i =0; i<n; i++) {
        cout << brr[i] << " ";
    }
    return 0;
}
cs

반응형