【数论】C046_天机锁(暴搜 / 枚举)

一、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();
    }
}

复杂度分析

  • 时间复杂度: O ( . . . ) O(...)
  • 空间复杂度: O ( . . . ) O(...)
原创文章 787 获赞 314 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/105845950