美文网首页
第8章 枚举算法

第8章 枚举算法

作者: 得力小泡泡 | 来源:发表于2020-03-30 17:56 被阅读0次

1、四平方和

算法分析

暴力做法,枚举abc,算出d,判断d是不是某个数的平方

时间复杂度 O(n^3)

Java 代码

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        //预处理
        for(int a = 0;a * a<= n;a ++)
            for(int b = a;a * a + b * b <= n;b ++)
                for(int c = b;a * a + b * b + c * c <= n;c ++)
                {
                    //巧妙方法,判定某个数是否能用其他数的平方表示出来
                    int t = n - a * a - b * b - c * c;
                    int d = (int)Math.sqrt(t);
                    if(d * d == t)
                    {
                        System.out.println(a + " " + b + " " + c + " " + d);
                        return;
                    }
                }
    }
}

2、装饰效果

算法分析

求最大区间和问题,dp

时间复杂度 O(n)

Java 代码

import java.util.Scanner;

public class Main{
    static int N = 1010;
    static int[] f = new int[N];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i = 1;i <= n;i ++)
            f[i] = scan.nextInt();
        
        int ans = 0;
        //求最大区间和
        for(int i = 1;i <= n;i ++)
        {
            f[i] = Math.max(f[i - 1] + f[i], f[i]);
            ans = Math.max(f[i], ans);
        }
        System.out.println(ans);
    }
}

3、奖劵数目

算法分析

感觉可以用数位dp,到时候试下
枚举 [n,m] 范围内的所有整数,对每个整数 x,取出它的每一位,判断是否有 4。如果每位都不存在 4,答案就加一。

时间复杂度 O(n)

Java 代码

import java.util.Scanner;

public class Main{
    static int N = 1010;
    static int[] f = new int[N];
    static boolean check(int x)
    {
        while(x > 0)
        {
            if(x % 10 == 4) return false;
            x /= 10;
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int res = 0;
        for(int i = n;i <= m;i ++)
        {
            if(check(i)) res ++;
        }
        System.out.println(res);
    }
}

4、双节棍

算法分析

选长度相差最短的两个棍子,排序,求差分,求出相邻两个数的差值的最下值

时间复杂度 O(n)

Java 代码

import java.util.Arrays;
import java.util.Scanner;

public class Main{
    static int N = 1010;
    static int[] a = new int[N];
    static int[] f = new int[N];//差分
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i = 1;i <= n;i ++) a[i] = scan.nextInt();
        Arrays.sort(a,1,n + 1);
        int res = 10000;
        for(int i = 2;i <= n;i ++) 
        {
            f[i] = a[i] - a[i - 1];
            res = Math.min(res, f[i]);
        }
        System.out.println(res);
        
    }
}

5、北极圈远征

算法分析

52 * (7x + 21k) = n,x从100枚举到1,k算一下上界是400,从1枚举到400

时间复杂度 O(n)

Java 代码

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int x = 100;x >= 1;x --)
        {
            for(int k = 1;k <= 400;k ++)
            {
                if(52 * (7 * x + 21 * k) == n) 
                {
                    System.out.println(x);
                    System.out.println(k);
                    return;
                }
            }
        }
    }
}

6、最大子阵和

算法分析

求最大子阵和问题,前缀和

时间复杂度 O(n^4)

Java 代码

import java.util.Scanner;

public class Main{
    static int N = 55;
    static int[][] f = new int[N][N];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        for(int i = 1;i <= n;i ++)
            for(int j = 1;j <= m;j ++)
            {
                f[i][j] = scan.nextInt();
                f[i][j] += f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] ;
            }
        int res = -1000;
        for(int x1 = 1; x1 <= n;x1 ++)
            for(int y1 = 1;y1 <= m;y1 ++)
                for(int x2 = 1;x2 <= x1;x2 ++)
                    for(int y2 = 1;y2 <= y1;y2 ++)
                    {
                        int v = f[x1][y1] - f[x2 - 1][y1] - f[x1][y2 - 1] + f[x2 - 1][y2 - 1];
                        res = Math.max(res,v);
                    }
        System.out.println(res);
        
    }
}

相关文章

  • 第15章 搜索枚举

    1、文具店 算法分析 暴力枚举每种情况,dfs(u,cnt)表示从第0个位置开始枚举,当前选第0个,当 cnt =...

  • 算法学习3_枚举

    枚举算法又称穷举算法枚举算法的核心思想 : 有序的尝试每一种可能 题一、 3 * 6528 = 3 * 8256 ...

  • 算法 | 枚举算法

    【算法思想】 利用计算机运算速度快的特点,对问题的所有可能答案一一列举,并逐一检验,符合条件的保留,不符合的丢弃。...

  • 第8章 枚举算法

    1、四平方和 算法分析 暴力做法,枚举a,b,c,算出d,判断d是不是某个数的平方 时间复杂度 Java 代码 2...

  • 2018-08-02

    php实现组合枚举算法 源码

  • 枚举算法

    枚举法:又称穷举法,是指从可能的集合中一一列举各个元素,用题目给定的约束条件判定哪些是无用的,哪些是有用的。能使命...

  • 枚举算法

    今天我们来讲一个万金油算法,这个算法可以解决所有的问题,它就是枚举法(穷举法)。 枚举算法是我们在日常中使用到的最...

  • 枚举算法

    枚举法的本质就是从所有候选答案中搜索正确的解,使用该算法需要满足两个条件: 可预先确定候选答案的数量。 候选答案的...

  • 枚举

    枚举 枚举算法又叫穷举算法。 基本思想是“有序地去尝试每一种可能”。例子:□□□+□□□=□□□,将数字1~9分别...

  • 回溯的例子-正则表达式

    回溯 回溯是比较强有力的遍历或枚举算法,掌握这个算法别无他途,只有勤学多练。在leetcode中的第10题就是一个...

网友评论

      本文标题:第8章 枚举算法

      本文链接:https://www.haomeiwen.com/subject/braluhtx.html