Untitled
raw download clone
C
views 55
,
size 606 b
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[100005] = {0};
int NUM(int id, int sum, int n, int k){ //id是a的位置,sum代表相加後的和,n k 的意思與題目相同
    if(sum == k) return 1; 
    if( id>=n && sum > k) return 0;
    return NUM(id+1,sum+a[id], n, k)+ NUM(id+1, sum, n, k);//搜尋符合的條件,想法是每個數字都選擇加或不加,前者代表有加
}
int main( ){
    int n, k, i;              
    scanf("%d%d", &n, &k);
    for(i=1;i<=n;i++){
        scanf("%d", &a[i]);
    }
    printf("%d\n", NUM(0,0, n, k));
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.