π₯ PS(Problem Solving) π₯/BOJ
[BOJ] #1182 λΆλΆμμ΄μ ν©
dar0m!
2019. 9. 4. 02:13
μκ° μ ν | λ©λͺ¨λ¦¬ μ ν | μ λ΅ λΉμ¨ |
2 μ΄ | 256 MB | 44.845 % |
1182λ²: λΆλΆμμ΄μ ν©
첫째 μ€μ μ μμ κ°μλ₯Ό λνλ΄λ Nκ³Ό μ μ Sκ° μ£Όμ΄μ§λ€. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) λμ§Έ μ€μ Nκ°μ μ μκ° λΉ μΉΈμ μ¬μ΄μ λκ³ μ£Όμ΄μ§λ€. μ£Όμ΄μ§λ μ μμ μ λκ°μ 100,000μ λμ§ μλλ€.
www.acmicpc.net
Nκ°μ μ μλ‘ μ΄λ£¨μ΄μ§ μμ΄μ΄ μμ λ, ν¬κΈ°κ° μμμΈ λΆλΆμμ΄ μ€μμ κ·Έ μμ΄μ μμλ₯Ό λ€ λν κ°μ΄ Sκ° λλ κ²½μ°μ μλ₯Ό ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
λ©λͺ¨λ¦¬ | μκ° |
1988 KB | 4 ms |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int n, s, ans;
int arr[30];
void func(int idx, int sum) {
if (n == idx) {
if (s == sum) {
ans++;
}
return;
}
func(idx + 1, sum + arr[idx]);
func(idx + 1, sum);
}
int main() {
scanf("%d%d", n, s);
for (int i = 0; i < n; i++) {
scanf("%d", arr[i]);
}
func(0, 0);
if (s == 0) ans--;
printf("%d", ans);
return 0;
}
|
cs |
λΉμ·νμ§λ§ λ ν¨μ¨μ μΈ λ°©λ²
λ©λͺ¨λ¦¬ | μκ° |
1116 KB | 0 ms |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h>
int n,s,cnt,a[30];
void find(int i,int sum){
if(sum+a[i]==s) cnt++;
if(i==n-1) return;
find(i+1,sum); find(i+1,sum+a[i]);
}
int main(){
scanf("%d %d",&n,&s);
for(int i=0; i<n; i++){
scanf("%d",&a[i]);
}
find(0,0);
printf("%d",cnt);
}
|
cs |