JAVA小练习(二)数字组合求和运算
程序员文章站
2022-10-01 18:41:55
JAVA小练习(二)一、题目描述1-9任意一个数字实现 ABC + DEF = GHI,其中ABCDEFGHI为不相同的0~9数字之一; 列出实现的式子和个数二、实现代码如下:public class water { public static void main(String[] args) { System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");...
JAVA小练习(二)
一、题目描述
1-9任意一个数字实现 ABC + DEF = GHI,其中ABCDEFGHI为不相同的0~9数字之一;
列出实现的式子和个数
二、实现代码如下:
public class water {
public static void main(String[] args) {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
System.out.println("1-9任意一个数字实现 ABC + DEF = GHI; 列出实现的式子和个数");
dfs1(1);
System.out.println("所得式子个数: " + count);
}
/**
* 1-9任意一个数字实现 ABC + DEF = GHI; 列出实现的式子和个数
*/
static int [] arr = new int[10];
static int [] result = new int[10];
static int step_total = 10;
static int count = 0;
public static void dfs1 (int step) {
//具体进行执行判断和输出
if (step >= step_total) {
int ABC = Integer.parseInt("" + arr[1] + arr[2] + arr[3]);
int DEF = Integer.parseInt("" + arr[4] + arr[5] + arr[6]);
int GHI = Integer.parseInt("" + arr[7] + arr[8] + arr[9]);
if (ABC + DEF == GHI) {
System.out.println(arr[1] + arr[2]+ arr[3] + "+"
+ arr[4] + arr[5] + arr[6] + "=" + arr[7]+ arr[8]+arr[9]);
count ++;
return;
}
}
//重复且不相等的生成arr[]
for (int i = 1; i < step_total; i++) {
if (result[i] == 0) {
result[i] = 1;
arr[step++] = i;
dfs1(step);
result[i] = 0;
}
}
}
}
本文地址:https://blog.csdn.net/qq_44140450/article/details/110205981