File tree Expand file tree Collapse file tree 3 files changed +61
-2
lines changed
Expand file tree Collapse file tree 3 files changed +61
-2
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <module type =" JAVA_MODULE" version =" 4" >
3+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4+ <exclude-output />
5+ <content url =" file://$MODULE_DIR$" >
6+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7+ </content >
8+ <orderEntry type =" inheritedJdk" />
9+ <orderEntry type =" sourceFolder" forTests =" false" />
10+ </component >
11+ </module >
Original file line number Diff line number Diff line change 1+ import java .io .BufferedReader ;
2+ import java .io .IOException ;
3+ import java .io .InputStreamReader ;
4+ import java .util .StringTokenizer ;
5+
6+
17public class Main {
2- public static void main (String [] args ) {
3- System .out .println ("Hello world!" );
8+ static int N ;
9+ static int S ;
10+ static int [] num ;
11+
12+
13+ static int count = 0 ;
14+
15+ public static void main (String [] args ) throws IOException {
16+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
17+ StringTokenizer st = new StringTokenizer (br .readLine ());
18+ N = Integer .parseInt (st .nextToken ());
19+ S = Integer .parseInt (st .nextToken ());
20+ num = new int [N ];
21+ st = new StringTokenizer (br .readLine ());
22+ int i = 0 ;
23+ while (st .hasMoreTokens ()) {
24+ num [i ] = Integer .parseInt (st .nextToken ());
25+ i ++;
26+ }
27+
28+ DFS (0 , 0 );
29+ if (S ==0 ){
30+ System .out .println (count -1 );
31+ }else {
32+ System .out .println (count );
33+ }
34+
35+
36+
37+ }
38+
39+ private static void DFS (int sum , int r ) {
40+
41+ if (N == r ) {
42+ if (sum == S ) {
43+ count ++;
44+ }
45+ return ;
46+ }
47+
48+ DFS (sum + num [r ], r + 1 ) ;
49+ DFS (sum , r + 1 ) ;
50+
451 }
552}
You can’t perform that action at this time.
0 commit comments