java循环穷举
答案:1 悬赏:0 手机版
解决时间 2021-03-25 03:40
- 提问者网友:龅牙恐龙妹
- 2021-03-24 04:27
java循环穷举
最佳答案
- 五星知识达人网友:duile
- 2021-03-24 05:46
代码如下:
public class Min_Number {
public static void main(String[] args) {
int num = 5;
int resa, resb, resc, resd, rese;
List list = new ArrayList<>();
for (int a = 1; a <= num; a++) {
resa = a * 10000;
for (int b = 1; b <= num; b++) {
if (b == a) {
continue;
}
resb = resa + b * 1000;
for (int c = 1; c <= num; c++) {
if (c == a || c == b) {
continue;
}
resc = resb + c * 100;
for (int d = 1; d <= num; d++) {
if (d == a || d == b || d == c) {
continue;
}
resd = resc + d * 10;
for (int e = 1; e <= num; e++) {
if (e == a || e == b || e == c || e == d) {
continue;
}
rese = resd + e * 1;
if (rese % 7 == 0) {
list.add(rese);
}
}
}
}
}
}
Object[] objects = list.toArray();
Arrays.sort(objects);
System.out.println("由1,2,3,4,5五个数字组成的能被7整除的最小五位数是:" + (int) objects[0]);
}
}运行效果:
public class Min_Number {
public static void main(String[] args) {
int num = 5;
int resa, resb, resc, resd, rese;
List
for (int a = 1; a <= num; a++) {
resa = a * 10000;
for (int b = 1; b <= num; b++) {
if (b == a) {
continue;
}
resb = resa + b * 1000;
for (int c = 1; c <= num; c++) {
if (c == a || c == b) {
continue;
}
resc = resb + c * 100;
for (int d = 1; d <= num; d++) {
if (d == a || d == b || d == c) {
continue;
}
resd = resc + d * 10;
for (int e = 1; e <= num; e++) {
if (e == a || e == b || e == c || e == d) {
continue;
}
rese = resd + e * 1;
if (rese % 7 == 0) {
list.add(rese);
}
}
}
}
}
}
Object[] objects = list.toArray();
Arrays.sort(objects);
System.out.println("由1,2,3,4,5五个数字组成的能被7整除的最小五位数是:" + (int) objects[0]);
}
}运行效果:
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯