[SW Expert] #3752 가능한 시험점수 :: 잡다한 프로그래밍
반응형
링크

https://swexpertacademy.com/main/main.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


1. 해결방법

처음에 for문을 2번 돌려서 해결하려 했지만 오류가 있었고 다른 분의 코드를 참고하였다

점수가 3가지가 입력되었다면 1번부터 가능한 모든 점수를 구하고, 다음번에 2번째 점수를 1번째 경우에 수에 모두 더하고,,,, 이런 방식으로 해결하였다


2. 코드

#include <iostream>
#include <stdio.h>
#include<string.h>
using namespace std;
 
int total = 0;
int num = 0;
int ary[100];
int result[10001];
int temp;
int cnt;
 
int main()
{
    scanf("%d", &total);
    for (int i = 0; i < total; i++) {
        memset(result, 0, sizeof(result));
        memset(ary, 0, sizeof(ary));
        result[0] = 1;
        cnt = 0; temp = 0;
         
    scanf("%d", &num);
        for (int j = 0; j < num; j++) {
        scanf("%d", &ary[j]);
            temp += ary[j];
            for (int n = temp; n >= 0; n--) {
                if (result[n]) {
                    result[n + ary[j]]++;
                }
            }
                        result[ary[j]]++;
        }
        for (int i = 0; i < 10001; i++) {
            if (result[i] > 0)
                cnt++;
        }
 
        printf("#%d %d\n", i+1, cnt);
    }
    return 0;
}
반응형

+ Recent posts