java刷题心得(3)之火柴棒等式

作者: 帅气的浮生 | 来源:发表于2018-01-04 19:29 被阅读11次
题目

其实这题不是特别难,我们一定要注重思路

首先我们就要想到,我们的加等式中的"+"和"="号都是要占用火柴棒的

一共要占用4根,

然后我们的整数最大值为24

减去4个 就是为20根火柴棒,那么20根组成的最大值就为11111

所以我们需要枚举11111个数字

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int total = 0;//记录满足要求的数量
        int c = 0;
        for (int i = 0; i <= 1111; i++)
            for (int j = 0; j <= 1111; j++) {
                c = i + j;//构造等式
                if (fun(c) + fun(i) + fun(j) == (n - 4)) {//如果满足要求,则总数+1
                    total++;
                }
            }
        System.out.println(total);
    }
    public static int fun(int a) {
        int[] arr = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; //定义的0-9的数字需要的火柴棒
        if (a == 0)//判断如果这个数字为0,直接返回所需要的火柴棒
            return arr[0];
        int sum = 0;
        while (a != 0) {
            sum += arr[a % 10];
            a /= 10;//把这个数字拆分,然后计算所需要的火柴棒
        }
        return sum;
    }
}

相关文章

网友评论

    本文标题:java刷题心得(3)之火柴棒等式

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