一、Problem
19811435
二、Solution
方法一:暴搜
低级失误:浪费了些许时间,一开始还看成了数字 2 只出现一次…
import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
static class Solution {
int[] a;
int N = 8;
int res;
boolean check() {
int x4 = 0, x9 = 0, sum = 0;
boolean x2 = false;
for (int i = 0; i < a.length; i++) {
if (a[i] == 4) x4++;
if (a[i] == 9) x9++;
if (a[i] == 2) x2 = true;
sum += a[i];
}
return x4 == x9 && x2 && sum <= 52;
}
void dfs(int k) {
if (k == N) {
if (check())
res++;
return;
}
for (int i = 0; i <= 9; i++) {
a[k] = i;
dfs(k+1);
}
}
void init() {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
a = new int[N];
dfs(0);
System.out.println(res);
}
}
public static void main(String[] args) throws IOException {
Solution s = new Solution();
s.init();
}
}
复杂度分析
- 时间复杂度: ,
- 空间复杂度: ,