https://www.acmicpc.net/problem/5585
C 풀이1
#include <stdio.h>
int main() {
int pay;
int one = 1000;
int left;
int count = 0;
scanf("%d", &pay);
left = one - pay;
while (left != 0) {
if (left >= 500) {
left -= 500;
count++;
}
else if (left >= 100) {
left -= 100;
count++;
}
else if (left >= 50) {
left -= 50;
count++;
}
else if (left >= 10) {
left -= 10;
count++;
}
else if (left >= 5) {
left -= 5;
count++;
}
else {
left -= 1;
count++;
}
}
printf("%d", count);
return 0;
}
C 풀이2
#include <stdio.h>
int main() {
int pay;
int one = 1000;
int left;
int count = 0;
scanf("%d", &pay);
left = one - pay;
for (; left >= 500;) {
left = left - 500;
count++;
}
for (; left >= 100;) {
left -= 100;
count++;
}
for (; left >= 50;) {
left -= 50;
count++;
}
for (; left >= 10;) {
left -= 10;
count++;
}
for(; left >= 5;) {
left -= 5;
count++;
}
for (; left >= 1;) {
left -= 1;
count++;
}
printf("%d", count);
return 0;
}
java
package javastd1;
import java.util.Scanner;
public class bj5585 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int one = 1000;
int left=0;
int cnt=0;
int pay = sc.nextInt();
left = one - pay;
while(left != 0) {
if (left >= 500) {
left -= 500;
cnt++;
}
else if (left >= 100) {
left -= 100;
cnt++;
}
else if (left >= 50) {
left -= 50;
cnt++;
}
else if (left >= 10) {
left -= 10;
cnt++;
}
else if (left >= 5) {
left -= 5;
cnt++;
}
else {
left -= 1;
cnt++;
}
}
System.out.println(cnt);
sc.close();
}
}
'공부하자 > 알고리즘' 카테고리의 다른 글
[백준] 2217번 로프 C, java (0) | 2022.01.06 |
---|---|
[백준] 10162번 전자레인지 C, java (0) | 2022.01.05 |
[백준] 1026번 보물 C, java (0) | 2021.12.30 |
[백준] 11399번 ATM C, java (0) | 2021.12.30 |
[백준] 14681번 사분면 고르기 C, java (0) | 2021.12.30 |