[BaekJoon] 32529번 - 래환이의 여자친구 사귀기 대작전 [Java][C++]
[BaekJoon] 32529번 - 래환이의 여자친구 사귀기 대작전 [Java][C++]
1. 문제 풀이
래환이가 최대한 늦게 다이어트를 시작하면서 목표 체중을 맞출 수 있는지 찾는 문제로 소개팅 날부터 역순으로 줄일 수 있는 체중을 더해나가서 가능한 순간 멈추면 가장 늦게 시작할 수 있다.
2. 코드
1. 풀이 [Java]
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[] arr = new int[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int sum = 0;
for (int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (sum >= m) {
System.out.println(i + 1);
return;
}
}
System.out.println(-1);
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> v(n);
for (int& x : v) cin >> x;
int sum = 0;
for (int i = n - 1; i >= 0; i--) {
sum += v[i];
if (sum >= m) {
cout << i + 1;
return 0;
}
}
cout << -1;
}
This post is licensed under CC BY 4.0 by the author.