您的位置:首页 > 编程语言

【算法】程序猿不写代码是不对的62

2017-05-25 17:44 309 查看
package com.kingdz.algorithm.time201705;

/**
* <pre>
* 摆火柴
* 有N个火柴,摆成A+B=C的形式
* 其中从0到9需要的火柴数分别是6,2,5,5,4,5,6,3,7,6
* 求根据给定的N求出可以有多少种摆法
* </pre>
*
* @author kingdz
*
*/
public class Algo25 {

private static int[] fire = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };

public static void main(String[] args) {
int n = 18;

int sum = 0;
for (int a = 0; a <= 1111; a++) {
for (int b = 0; b < 1111; b++) {
int c = a + b;
if (fn(a) + fn(b) + fn(c) == n - 4) {
System.out.println("" + a + "+" + b + "=" + c + "");
sum++;
}
}
}
System.out.println(sum);
}

/**
* 返回数X需要的火柴数目
*
*/
private static int fn(int x) {
int count = 0;
String num = "" + x;
for (char c : num.toCharArray()) {
int index = Integer.parseInt("" + c);
count = count + fire[index];
}
return count;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: