简单的迭代算法
时间:2021-3-24 15:24
热度:1164°
评论:0 条
如题所示:
我分别采用了一下几种方式去实现
采用随机算法:
public class Demo { public static void main(String[] args) { for (int i = 0; i < 1000000; i++) { System.out.println("第"+i+"次"); int[] list = getList(); int A = list[0]; int B = list[1]; int C = list[2]; int D = list[3]; int E = list[4]; int F = list[5]; int G = list[6]; int H = list[7]; int I = list[8]; int J = list[9]; //规则1:六边形的对角线的三个数之和相等 Integer rule1 = B + I; if (rule1 != C + H) { continue; } if (rule1 != D + F) { continue; } //规则2:三边上的四个数字之和相等 Integer rule2 = A + B + D + G; if (rule2 != A + C + F + J ) { continue; } if (rule2 != G + H + I + J) { continue; } System.out.println("a:"+A+";b:"+B+";c:"+C+";d:"+D+";e:"+E+";f:"+F+";g:"+G+";h:"+H+";i:"+I+";j:"+J); return; } System.out.println("程序结束!!无结果"); } //随机生成0~10 private static int[] getList() { int[] arr = new int[10]; int count = 0; while (count < arr.length) { int num = (int) (Math.random() * 10 + 1); //给num随机赋值 boolean b = true; for (int i = 0; i < arr.length; i++) { //判断arr数组中,是否已经存在num的值,如果是,b=false,如果不存在,进入下一个if语句中 if (arr[i] == num) { b = false; } } if (b == true) { arr[count] = num; //如果num不存在arr数组中,将num赋值给arr数组 count++; } } return arr; } }采用For循环暴力算法:
Int32[] a = new Int32[10];//是否已被使用 Int32[] i = new Int32[10];//第几个数字 Int32 temp; Int32 Count = 0;//统计运算次数 Int32 Result = 0; for (i[0] = 0; i[0] < 10; i[0]++) { a[i[0]] = 1; for (i[1] = 0; i[1] < 10; i[1]++) { if (a[i[1]] == 1) continue; a[i[1]] = 1; for (i[2] = 0; i[2] < 10; i[2]++) { if (a[i[2]] == 1) continue; a[i[2]] = 1; for (i[3] = 0; i[3] < 10; i[3]++) { if (a[i[3]] == 1) continue; a[i[3]] = 1; for (i[4] = 0; i[4] < 10; i[4]++) { if (a[i[4]] == 1) continue; a[i[4]] = 1; for (i[5] = 0; i[5] < 10; i[5]++) { if (a[i[5]] == 1) continue; a[i[5]] = 1; for (i[6] = 0; i[6] < 10; i[6]++) { if (a[i[6]] == 1) continue; a[i[6]] = 1; for (i[7] = 0; i[7] < 10; i[7]++) { if (a[i[7]] == 1) continue; a[i[7]] = 1; for (i[8] = 0; i[8] < 10; i[8]++) { if (a[i[8]] == 1) continue; a[i[8]] = 1; for (i[9] = 0; i[9] < 10; i[9]++) { if (a[i[9]] == 1) continue; a[i[9]] = 1; temp = i[3] + i[5]; if (temp == i[7] + i[2] && temp == i[1] + i[8]) { temp = i[0] + i[1] + i[3] + i[6]; if (temp == i[6] + i[7] + i[8] + i[9] && temp == i[0] + i[2] + i[5] + i[9]) { Result++; Debug.Print(" {0}\n {1} {2}\n {3} {4} {5}\n{6} {7} {8} {9}", i[0] + 1, i[1] + 1, i[2] + 1, i[3] + 1, i[4] + 1, i[5] + 1, i[6] + 1, i[7] + 1, i[8] + 1, i[9] + 1); } } Count++; a[i[9]] = 0; } a[i[8]] = 0; } a[i[7]] = 0; } a[i[6]] = 0; } a[i[5]] = 0; } a[i[4]] = 0; } a[i[3]] = 0; } a[i[2]] = 0; } a[i[1]] = 0; } a[i[0]] = 0; }
运行结果:
采用迭代算法:
#include <stdio.h> #include <math.h> int a[10]={0};//是否已被使用 int i[10]={0};//第几个数字 int temp; int Count = 0;//统计运算次数 int Result = 0; void ForEach(int n){ for(i[n]=0;i[n]<10;i[n]++){ if(a[i[n]] == 1) continue; a[i[n]] = 1; if(n<9){ ForEach(n+1); }else if(n==9){ temp = i[3] + i[5]; if (temp == i[7] + i[2] && temp == i[1] + i[8]) { temp = i[0] + i[1] + i[3] + i[6]; if (temp == i[6] + i[7] + i[8] + i[9] && temp == i[0] + i[2] + i[5] + i[9]) { Result++; printf(" %d\n %d %d\n %d %d %d\n%d %d %d %d", i[0] + 1, i[1] + 1, i[2] + 1, i[3] + 1, i[4] + 1, i[5] + 1, i[6] + 1, i[7] + 1, i[8] + 1, i[9] + 1);//打印结果 } } Count++; } a[i[n]] = 0; } } int main() { ForEach(0); printf("共统计了%d次,符合的结果共%d个\n", Count, Result); return 0; }采用python工具包
import itertools ret = [p for p in list(itertools.permutations(list(range(1, 11)), 10)) if ((sum([p[0], p[1], p[3], p[6]]) == sum([p[0], p[2], p[5], p[9]]) == sum([p[6], p[7 ], p[8], p[9]])) and (sum([p[1], p[4], p[8]]) == sum([p[2], p[4], p[ 7]]) == sum([p[3], p[4], p[5]])))] print(ret) print(len(ret))运行结果:
[(1, 6, 10, 5, 4, 8, 9, 3, 7, 2), (1, 10, 6, 8, 4, 5, 2, 7, 3, 9), (2, 6, 8, 5, 7, 4, 10, 1, 3, 9), (2, 7, 8, 3, 4, 10, 9, 5, 6, 1), (2, 8, 6, 4, 7, 5, 9, 3, 1, 10), (2, 8, 7, 10, 4, 3, 1, 6, 5, 9), (9, 3, 4, 1, 7, 8, 10, 5, 6, 2), (9, 3, 5, 7, 4, 6, 2, 8, 10, 1), (9, 4, 3, 8, 7, 1, 2, 6, 5, 10), (9, 5, 3, 6, 4, 7, 1, 10, 8, 2), (10, 1, 5, 3, 7, 6, 9, 4, 8, 2), (10, 5, 1, 6, 7, 3, 2, 8, 4, 9)] 12对比上述几种算法不难看出:
随机算法不能保证算出全部的结果。
采用For循环暴力算法代码可读性不高。
迭代算法基于For循环暴力算法,代码简洁。
python工具包的itertools提供了非常有用的用于操作迭代对象的函数,虽然代码最为简洁,但无法理解程序上是如何实现这个算法的底层逻辑。
捐赠支持:如果觉得这篇文章对您有帮助,请“扫一扫”鼓励作者!
相关文章
本文无需标签!
发表吐槽
你肿么看?
既然没有吐槽,那就赶紧抢沙发吧!