기본 콘텐츠로 건너뛰기

[BOJ 2293]동전1 - DP에서 중복을 제외하는 법

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main(void)
{
    int N,K;
    int result[10001] ={0,};
    scanf("%d %d",&N,&K);
    vector<int> vec;
    for (int i=0; i<N; i++){
        int input;
        scanf("%d",&input);
        vec.push_back(input);
    }
    
    //중복을 제외하는 법은 아래와 같이 한번만 계산해야될 부분을 바깥 loop에 감싼다.
    for (int i=0;i<vec.size();i++)
    {
        for (int j=1;j<=K;j++){
            if ((j-vec[i]) < 0)
                continue;
            if ((j-vec[i]) == 0)
                result[j]+=1;
            else
                result[j]+=result[j-vec[i]];
        }
    }
    printf("%d",result[K]);
}

댓글

이 블로그의 인기 게시물

Tree traversal의 3가지

1. 전위 순회 (Preorder Traversal) Root -> Left Tree -> Right Tree   ( 루트를 제일 처음에 방문 ) 2. 중위 순회 (Inorder Traversal) Left Tree -> Root -> Right Tree   ( 루트를 중간에 방문 ) 3. 후위 순회 (Postorder Traversal) Left Tree -> Right Tree -> Root   ( 루트를 제일 마지막에 방문 ) <소스코드> 출처 : http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder /// C program for different tree traversals #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child    and a pointer to right child */ struct node {      int data;      struct node* left;      struct node* right; }; /* Helper function that allocates a new node with the    given data and NULL left and right pointers. */ struct node* newNode(int data) {      struct node* node = (struct node*)                 ...