SMALL
https://www.acmicpc.net/problem/11047
11047번: 동전 0
첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000) 둘째 줄부터 N개의 줄에 동전의 가치 Ai가 오름차순으로 주어진다. (1 ≤ Ai ≤ 1,000,000, A1 = 1, i ≥ 2인 경우에 Ai는 Ai-1의 배수)
www.acmicpc.net
- 문제
- 문제풀이
큰 단위로 뒤집어서 큰것부터 빼주었다.
- 코드 1
using System.Text;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split();
int N = int.Parse(input[0]);
int K = int.Parse(input[1]);
int count = 0;
int coincnt = 0;
int[] arr=new int[N];
for(int i = 0; i < N; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
Array.Reverse(arr);
for(int i=0;i<N;i++)
{
if (K / arr[i] >= 1&&K!=0)
{
coincnt= K / arr[i];
count += coincnt;
K = K % arr[i];
}
else if (K == 0)
{
break;
}
}
Console.WriteLine(count);
}
}
}
- 후기
동전문제는 이제 그리디로 쉽게 하는 것 같다.
LIST
'백준 > C#' 카테고리의 다른 글
[C#]백준 15649번: N과 M(1) (0) | 2023.07.19 |
---|---|
[C#]백준 1541번: 잃어버린 괄호 (0) | 2023.07.19 |
[C#]백준 13305번: 주유소 (0) | 2023.07.19 |
[C#]백준 11399번: ATM (0) | 2023.07.19 |
[C#]백준 1931번: 회의실 배정 (0) | 2023.07.19 |