三羊献瑞 蓝桥杯java_第六届蓝桥杯Java B——三羊献瑞

观察下面的加法算式:

fe612e04b7e32e5df51de51e1630ee36.png

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

请你填写“三羊献瑞”所代表的4位数字(答案唯一)public class Main {

static int[] book = new int[10];

static int[] res = new int[8];

public static void main(String[] args) {

dfs(0);

}

/*

* 祥瑞生辉三羊献气 res[0]~res[7]

*/

static void dfs(int idx) {

if (idx == 8) {

if (check()) {

for (int i : res)

System.out.print(i + " ");

System.out.println();

}

return;

}

for (int i = 0; i <= 9; i++) {

if (book[i] == 0) {

book[i] = 1;

res[idx] = i;

dfs(idx + 1);

book[i] = 0;

}

}

}

static boolean check() {

int a = res[0] * 1000 + res[1] * 100 + res[2] * 10 + res[3];

int b = res[4] * 1000 + res[5] * 100 + res[6] * 10 + res[1];

int c = res[4] * 10000 + res[5] * 1000 + res[2] * 100 + res[1] * 10 + res[7];

return a + b == c && res[0] != 0 && res[4] != 0;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值