[BaekJoon] #10178 - 할로윈의 사탕 [Java][C++]
[BaekJoon] #10178 - 할로윈의 사탕 [Java][C++]
1. 아이디어
사탕을 나누는 것은 몫으로, 남은 사탕은 나머지를 구하는 것으로 간단하게 구할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(T)$ | $O(1)$ |
($T$ = 테스트 케이스 수)
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
sb.append("You get ").append(c / v).append(" piece(s) and your dad gets ").append(c % v).append(" piece(s).\n");
}
System.out.println(sb);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int c, v;
cin >> c >> v;
cout << "You get " << (c / v) << " piece(s) and your dad gets " << (c % v) << " piece(s).\n";
}
}
This post is licensed under CC BY 4.0 by the author.