본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 9848 : Gift

반응형

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

 

9848번: Gift

The first line contains 2 integers n and k, where n (3 ≤ n ≤ 100) is the number of days, and k (0 < k ≤ 100,000) the desired improvement (in milliseconds). Whenever Jacqueline’s timing reduces by at least k milliseconds over the previous day’s ti

www.acmicpc.net

간단 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

기록한 날 수 n, 선물을 받기 위한 기록단축 크기 k, 정답을 출력할 ans, 그 날 기록 record, 이전날 기록 prevRecord를 선언한 후 입력받습니다.

📔 풀이과정

이전날 기록 - 현재날 기록 >= 기록단축 크기라면 ans++해줍니다.

📔 정답출력

ans를 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
int n, k, ans, record, prevRecord;
int main(){
  cin >> n >> k;
  while(n--){
    cin >> record;
    if(prevRecord - record >= k) ans++;
    prevRecord = record;
  }
  cout << ans;
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.