王守恩
发表于 2020-2-12 19:05:43
dlpg070 发表于 2020-2-12 13:50
挑战题又来了
1个已知仅仅3数的例子,求4x4普通幻方
多解,有点多,很有趣,有挑战性
条件太少了,还是旁边的小朋友做的。
要不来点提示,a23+a32,a23-a32 都行。
+----+----+----+----+
|1|4| 13 | 16 |
+----+----+----+----+
| 14 | 15 |2|3|
+----+----+----+----+
| 8|5|12 |9|
+----+----+----+----+
| 11 | 10 |7|6| |
+----+----+----+----+
王守恩
发表于 2020-2-13 10:56:49
王守恩 发表于 2020-2-12 19:05
条件太少了,还是旁边的小朋友做的。
要不来点提示,a23+a32,a23-a32 都行。
再来 1 个,还是旁边的小朋友做的。
+----+----+----+----+
|1|14| 8 |11 |
+----+----+----+----+
|4|15 |5| 10 |
+----+----+----+----+
| 13|2| 12 |7|
+----+----+----+----+
| 16|3|9|6|
+----+----+----+----+
王守恩
发表于 2020-2-13 11:16:56
dlpg070 发表于 2020-2-12 13:50
挑战题又来了
1个已知仅仅3数的例子,求4x4普通幻方
多解,有点多,很有趣,有挑战性
能说下面的题是无解的吗?
+----+----+----+----+
| | 1|2| |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
dlpg070
发表于 2020-2-13 11:28:10
王守恩 发表于 2020-2-12 19:05
条件太少了,还是旁边的小朋友做的。
要不来点提示,a23+a32,a23-a32 都行。
为什么33#的答案给25分?
为了强调此题有4个解,只答了1个,即1/4,按100分制,只能给25分
希望分析研判全部解,这部分较难,解越多越难
谈谈问题难度设计的考虑:
本主题原题难度很大
有6个解还没有人能手工解决
为了使讨论能逐渐深入,我设计了一系列问题,由易到难,难度逐渐加大
例如:
14#已知8数 唯一解
22#已知7数 唯一解
26#已知6数 唯一解 评语:漂亮
28#已知3数 2个解挑战题来了 评语:好汉
31#已知3数 4个解挑战题又来了 评语:不错,---
随着难度增加,王守恩越战越勇,渐入佳境
但31#的回答有点“飘”,忘了分析多解,有点遗憾
1# 已知4数 6个解原题 按难度排在这里,至今没人能手工求解
原计划高难题目:
已知2数 32个解知名的丢勒幻方 可能解个数(幻方底部中间为15 14)
各位网友试试看,手工或编程都可以,盼望奇迹出现.
dlpg070
发表于 2020-2-13 12:02:31
本帖最后由 dlpg070 于 2020-2-13 21:28 编辑
王守恩 发表于 2020-2-13 11:16
能说下面的题是无解的吗?
+----+----+----+----+
把数字改一下就有解了,而且我认为手工不可能得到全部解,想挑战吗?
恰巧是我计划的高难度题目(15 14 所在行不同)
+----+----+----+----+
| | 15 | 14 | |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
王守恩
发表于 2020-2-13 14:37:37
本帖最后由 王守恩 于 2020-2-13 15:25 编辑
dlpg070 发表于 2020-2-13 12:02
把数字改一下就有解了,而且我认为手工不可能得到全部解,想挑战吗?
+----+----+----+----+
...
+-----+-----+------+-----+
| a11 |15|14| a14 |
+-----+-----+------+-----+
| a21 | a22 | a23 | a24 |
+-----+-----+-----+------+
| a31 | a32 | a33 | a34 |
+-----+-----+-----+------+
| a41 | a42 | a43 | a44 |
+-----+-----+-----+------+
a11可以有 1, 2, 3, 4, 四种填法。
一,a11=1时,有5种可能。
1,(a21,a31,a41)+(a22,a33,a44)=(5,12,16)+(09,11,13)
2,(a21,a31,a41)+(a22,a33,a44)=(6,11,16)+(08,12,13)
3,(a21,a31,a41)+(a22,a33,a44)=(7,10,16)+(08,12,13)
4,(a21,a31,a41)+(a22,a33,a44)=(7,10,16)+(09,11,13)
5,(a21,a31,a41)+(a22,a33,a44)=(8,09,16)+(10,11,12)
二,a11=2时,有8种可能。
1,(a21,a31,a41)+(a22,a33,a44)=(4,12,16)+(8,11,13)
2,(a21,a31,a41)+(a22,a33,a44)=(4,12,16)+(9,10,13)
3,(a21,a31,a41)+(a22,a33,a44)=(5,11,16)+(7,12,13)
4,(a21,a31,a41)+(a22,a33,a44)=(5,11,16)+(9,10,13)
5,(a21,a31,a41)+(a22,a33,a44)=(6,10,16)+(7,12,13)
6,(a21,a31,a41)+(a22,a33,a44)=(6,10,16)+(8,11,13)
7,(a21,a31,a41)+(a22,a33,a44)=(6,10,16)+(9,11,12)
8,(a21,a31,a41)+(a22,a33,a44)=(7,09,16)+(8,11,13)
三,a11=3 即 a14=2时,有8种可能。
1,(a24,a34,a44)+(a23,a32,a41)=(4,12,16)+(8,11,13)
2,(a24,a34,a44)+(a23,a32,a41)=(4,12,16)+(9,10,13)
3,(a24,a34,a44)+(a23,a32,a41)=(5,11,16)+(7,12,13)
4,(a24,a34,a44)+(a23,a32,a41)=(5,11,16)+(9,10,13)
5,(a24,a34,a44)+(a23,a32,a41)=(6,10,16)+(7,12,13)
6,(a24,a34,a44)+(a23,a32,a41)=(6,10,16)+(8,11,13)
7,(a24,a34,a44)+(a23,a32,a41)=(6,10,16)+(9,11,12)
8,(a24,a34,a44)+(a23,a32,a41)=(7,09,16)+(8,11,13)
四,a11=4 即 a14=1时,有5种可能。
1,(a24,a34,a44)+(a23,a32,a41)=(5,12,16)+(09,11,13)
2,(a24,a34,a44)+(a23,a32,a41)=(6,11,16)+(08,12,13)
3,(a24,a34,a44)+(a23,a32,a41)=(7,10,16)+(08,12,13)
4,(a24,a34,a44)+(a23,a32,a41)=(7,10,16)+(09,11,13)
5,(a24,a34,a44)+(a23,a32,a41)=(8,09,16)+(10,11,12)
王守恩
发表于 2020-2-14 15:03:29
本帖最后由 王守恩 于 2020-2-14 15:04 编辑
王守恩 发表于 2020-2-13 14:37
+-----+-----+------+-----+
| a11 |15|14| a14 |
+-----+-----+------+-----+ ...
+-----+-----+------+-----+
| a11 |15|14| a14 |
+-----+-----+------+-----+
| a21 | a22 | a23 | a24 |
+-----+-----+-----+------+
| a31 | a32 | a33 | a34 |
+-----+-----+-----+------+
| a41 | a42 | a43 | a44 |
+-----+-----+-----+------+
a11可以有 1, 2, 3, 4(同时4,3,2,1只能填在a14) 四种填法。
1, 2, 3, 4填好后,1,4对应的2,3、2,3对应的1,4 只能填在a42,a43(其他位置无法填),
a42,a43填2,3、1,4后,a41,a44只能填13,16,
a22可能填5,6,7,8,9,10,11,12八种可能
共计4×2×2×8=128种可能。对这128种可能逐一试填,结果如下。
01,1+4+2+3+16+13+08+09+12+05+10+07+06+11
02,1+4+2+3+16+13+08+09+12+05+11+06+07+10
03,1+4+2+3+16+13+09+08+11+06+07+10+05+12
04,1+4+2+3+16+13+09+08+11+06+12+05+10+07
05,1+4+2+3+16+13+11+06+09+08+05+12+07+10
06,1+4+2+3+16+13+11+06+09+08+10+07+12+05
07,1+4+2+3+16+13+12+05+08+09+06+11+10+07
08,1+4+2+3+16+13+12+05+08+09+07+10+11+06
09,1+4+3+2+13+16+06+10+11+07+12+08+05+09
10,1+4+3+2+13+16+10+06+07+11+08+12+09+05
11,2+3+1+4+16+13+08+10+11+05+09+07+06+12
12,2+3+1+4+16+13+10+08+09+07+05+11+06+12
13,2+3+1+4+16+13+10+08+09+07+12+06+11+05
14,2+3+1+4+16+13+12+06+07+09+05+11+10+08
15,2+3+4+1+13+16+05+10+11+08+12+07+06+09
16,2+3+4+1+13+16+09+06+07+12+08+11+10+05
17,3+2+1+4+13+16+06+12+09+07+10+08+05+11
18,3+2+1+4+13+16+08+10+07+09+06+12+05+11
19,3+2+1+4+13+16+08+10+07+09+12+06+11+05
20,3+2+1+4+13+16+10+08+05+11+06+12+09+07
21,3+2+4+1+16+13+06+09+12+07+10+05+08+11
22,3+2+4+1+16+13+10+05+08+11+06+09+12+07
23,4+1+2+3+13+16+05+12+09+08+10+07+06+11
24,4+1+2+3+13+16+05+12+09+08+11+06+07+10
25,4+1+2+3+13+16+06+11+08+09+07+10+05+12
26,4+1+2+3+13+16+06+11+08+09+12+05+10+07
27,4+1+2+3+13+16+08+09+06+11+05+12+07+10
28,4+1+2+3+13+16+08+09+06+11+07+10+05+12
29,4+1+2+3+13+16+09+08+05+12+06+11+10+07
30,4+1+2+3+13+16+09+08+05+12+07+10+11+06
31,4+1+3+2+16+13+06+10+11+07+09+05+08+12
32,4+1+3+2+16+13+10+06+07+11+05+09+12+08
上述14个数表示
a11+a14+a42+a43+a41+a44+a22+a32+a33+a23+a21+a31+a34+a24
dlpg070
发表于 2020-2-14 15:55:04
本帖最后由 dlpg070 于 2020-2-14 16:19 编辑
王守恩 发表于 2020-2-14 15:03
+-----+-----+------+-----+
| a11 |15|14| a14 |
+-----+-----+------+-----+...
最近发现chyanog的解4阶幻方的c++代码,简单快速,适于解本主题各例题
我增加菜单,改变输入输出,把结果保存到文件,
解一道题仅需 0.2 秒(包括输出到文件)
解全部8个例题(包括全部7040解),用时不到2秒
代码如下,供参考
// chyanog.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
// 四阶幻方全生成问题 - 第2页 - 算法交流 - 数学研发论坛
// https://bbs.emath.ac.cn/forum.php?mod=viewthread&tid=224&extra=&highlight=%CB%C4%BD%D7%BB%C3%B7%BD&page=2
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int select, ret;
char buf;
char str;
FILE *fp;
errno_t err;
err = fopen_s(&fp,"outch.txt", "wb");
sprintf_s(str, "已知0---16个数字求全部4x4幻方,多个实例\n");
fwrite(str, 1, strlen(str), fp);
fflush(fp);
ret = 0;
while (ret == 0)
{
printf("\n 已知0---16个数字求全部4x4幻方\n\n");
printf("\n==================\n主菜单\n================\n\n");
printf("1. 14#已知8数\n");
printf("2. 22#已知7数\n");
printf("3. 26#已知6数\n");
printf("4. 28#已知3数挑战题来了\n");
printf("5. 31#已知3数挑战题又来了\n");
printf("6. 1# 已知4数原题 \n");
printf("7. 37#已知2数(15 14)高难度 \n");
printf("8. 已知0数全部解7040 \n");
printf("0 或q 退出\n\n");
printf(" 请输入您的选择:");
gets_s(buf);
select = atoi(buf);
printf("\n");
if (select==99 || select == 0)
{
fclose(fp);
return 1;
}
sprintf_s(str, "\n=======\nselect= %d\n", select);
fwrite(str, 1, strlen(str), fp);
clock_t t0 = clock();
int a11,a12,a13,a14;
int a21,a22,a23,a24;
int a31,a32,a33,a34;
int a41,a42,a43,a44;
int i1, i2, i3, i4, i5, i6, i7;
int cnt = 0;
for (i1 = 1; i1 <= 16; i1++)
{
for (i2 = 1; i2 <= 16; i2++)
{
if (i2 == i1) continue;
for (i3 = 1; i3 <= 16; i3++)
{
if (i1 + i2 + i3 > 33 || i1 + i2 + i3 < 18 || i1 == i3 || i2 == i3 || 2 * i1 + i2 + i3 == 34 || i1 + 2 * i2 + i3 == 34 || i1 + i2 + 2 * i3 == 34) continue;
for (i4 = 1; i4 <= 16; i4++)
{
if (i1 == i4 || i2 == i4 || i3 == i4 || i1 + i2 + i3 + i4 == 34) continue;
for (i5 = 1; i5 <= 16; i5++)
{
if (i1 == i5 || i2 == i5 || i3 == i5 || i4 == i5 || i1 + i2 + i3 + i5 == 34 || 2 * i1 + 2 * i2 + i3 + i4 + 2 * i5 == 68) continue;
for (i6 = 1; i6 <= 16; i6++)
{
if (i4 + i5 + i6 > 33 || i4 + i5 + i6 < 18 || i1 == i6 || i2 == i6 || i3 == i6 || i4 == i6 || i5 == i6 || i1 + i2 + i3 + i6 == 34 || i1 + i2 + i3 == i4 + i5 + i6 || i3 + i4 + i5 + i6 == 34 || i1 + i4 + i5 + i6 == 34 || i2 + i4 + i5 + i6 == 34 || 2 * i1 + i2 + i3 + i4 == 34 + i6 || i1 + i5 == i3 + i6 || i1 + 2 * i2 + i3 + i5 == 34 + i6 || 2 * i1 + i2 + i4 + i5 == 34 + i6 || 2 * i4 + i5 + i6 == 34 || i4 + 2 * i5 + i6 == 34 || i4 + i5 + 2 * i6 == 34 || 2 * i1 + i2 + i3 + i4 + 2 * i5 + i6 == 68) continue;
for (i7 = 1; i7 <= 16; i7++)
{
if (2 * i1 + i2 + i4 + i5 + i7 > 50 + i6 || 35 + i6 > 2 * i1 + i2 + i4 + i5 + i7 || 2 * i1 + 2 * i2 + i3 + i4 + i5 + i7 > 67 + i6 || 52 + i6 > 2 * i1 + 2 * i2 + i3 + i4 + i5 + i7 || 2 * i1 + i2 + i3 + i4 + i7 > 50 + i6 || 35 + i6 > 2 * i1 + i2 + i3 + i4 + i7 || 2 * i1 + i2 + i3 + i4 + i5 + i7 < 52 || 2 * i1 + i2 + i3 + i4 + i5 + i7 > 67 || i1 + i2 + i3 + i4 + i7 < 35 || i1 + i2 + i3 + i4 + i7 > 50 || i1 + i4 + i7 < 18 || i1 + i4 + i7 > 33 || 1 + i7 > i5 + i6 || i5 + i6 > 16 + i7 || i4 + i5 + i6 + i7 == 34 || i1 + i2 + i3 + i7 == 34 || i6 == i7 || i5 == i7 || i4 == i7 || i3 == i7 || i2 == i7 || i1 == i7 || i1 + i4 + i6 + i7 == 34 || i2 + i3 == i4 + i7 || i2 + i3 + i4 + i7 == 34 || i3 + i7 == i5 + i6 || i2 + i7 == i5 + i6 || i1 + i7 == i5 + i6 || i1 + i2 + i3 + i5 + i6 == 34 + i7 || i1 + i3 + i4 + i7 == 34 || i1 + i2 + i4 + i7 == 34 || i1 + i4 + i5 + i7 == 34 || i1 + i2 + i3 + i4 + i7 == 34 + i5 || i4 + i7 == i5 + i6 || i4 + 2 * (i5 + i6) == 34 + i7 || i1 + i2 + i3 + 2 * i4 + i5 + i6 + i7 == 68 || 2 * i1 + i2 + i3 + i4 + i5 + i6 + i7 == 68 || i1 + i2 + i3 + i4 + i7 == 34 + i6 || 2 * i1 + i2 + i3 + i7 == 34 + i6 || 2 * i1 + i2 + 2 * i3 + i4 + i5 + i7 == 68 || i1 + 2 * i4 + i7 == 34 || 2 * i1 + 2 * i2 + i3 + i4 + i5 + i7 == 68 || i1 + i2 + i4 + i5 + i7 == 34 + i6 || 2 * i1 + i2 + i3 + i4 + i5 + 2 * i7 == 68 || 2 * i1 + i4 + i7 == 34 || i1 + i4 + 2 * i7 == 34 || 2 * i1 + 2 * i2 + 2 * i3 + i4 + i7 == 68 || 2 * i1 + i2 + i3 + i4 + i7 == 34 + i5 + i6 || 2 * i1 + i3 + i4 + i7 == 34 + i6 || 2 * i1 + i2 + i3 + 2 * i4 + i5 + i7 == 68 || 2 * i1 + i2 + i4 + i7 == 34 + i6 || i5 + i6 == 2 * i7 || 2 * i1 + i2 + i3 + i4 + 2 * i5 + i7 == 68 || 2 * i1 + i2 + i5 + i7 == 34 + i6 || i1 + i2 + i3 + i4 + 2 * i7 == 34 + i5 + i6 || 2 * i1 + 2 * i2 + 2 * i3 + i4 + i5 + i7 == 68 + i6 || 2 * i1 + 2 * i2 + i3 + i7 == 34 + 2 * i6 || 2 * i1 + i2 + 2 * i4 + 2 * i5 + i7 == 68 || 2 * i1 + i4 + i5 + i7 == 34 + i6 || 3 * i1 + i2 + i3 + i4 + i5 + i7 == 68 || 2 * i1 + i2 + i3 + i4 + 2 * i7 == 34 + i5 + 2 * i6 || 2 * i1 + 2 * i2 + i3 + i4 + i5 + 2 * i7 == 68 + i6 || 3 * i1 + 2 * i2 + 2 * i3 + i4 + i7 == 68 + i6 || 2 * i1 + 2 * i2 + i3 + 2 * i4 + i5 + i7 == 68 + i6 || 2 * i1 + 3 * i2 + i3 + i4 + i5 + i7 == 68 + i6 || 2 * i1 + i2 + i3 + i4 + i7 == 34 + 2 * i6 || 2 * i1 + i2 + i4 + i5 + i7 == 34 + i3 + i6 || 2 * i1 + i2 + i3 + 2 * i4 + 2 * i7 == 68 || 2 * i1 + 2 * i2 + i3 + i4 + 2 * i5 + i7 == 68 + i6 || 3 * i1 + 2 * i2 + i3 + i4 + i5 + i7 == 68 + i6 || 2 * i1 + i2 + i4 + 2 * i7 == 34 + 2 * i6 || 3 * i1 + 2 * i2 + 2 * i3 + 2 * i4 + i5 + 2 * i7 == 102 || 2 * i1 + i2 + i4 + i5 + i7 == 34 + 2 * i6 || 3 * i1 + i2 + i3 + 2 * i4 + 2 * i7 == 68 + i6 || 3 * i1 + 3 * i2 + 2 * i3 + 2 * i4 + i5 + 2 * i7 == 102 + i6 || 3 * i1 + i2 + 2 * i4 + i5 + 2 * i7 == 68 + i6 || 4 * i1 + 2 * i2 + i3 + 2 * i4 + 2 * i5 + 2 * i7 == 102 + i6 || 4 * i1 + 2 * i2 + 2 * i3 + 2 * i4 + i5 + 2 * i7 == 102 + i6 || 4 * i1 + 3 * i2 + 2 * i3 + 2 * i4 + i5 + 2 * i7 == 2 * (51 + i6) || 4 * i1 + 3 * i2 + i3 + 2 * i4 + 2 * i5 + 2 * i7 == 2 * (51 + i6)) continue;
a11=i1;a12= i2;a13= i3;a14= 34 - i1 - i2 - i3;
a21=i4; a22=i5;a23= i6;a24= 34 - i4 - i5 - i6;
a31=i7;a32= -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7;
a33= 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7;a34= i5 + i6 - i7;
a41= 34 - i1 - i4 - i7;a42= 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7;
a43= -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7;
a44= -34 + i1 + i2 + i3 + i4 + i7;
switch (select)
{
case 1://"1. 14#已知8数
if (a21==5&&a22==10&&a23==11&&a24==8&&a41==14&&a42==4&&a43==1&&a44==15)
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 2://2. 22#已知7数
if (a11 == 16 && a12 == 15 && a13 == 2 && a21 == 5 && a22 == 3 && a23 == 14 && a31 == 4)
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 3://3. 26#已知6数
if (a11 == 10 && a12 == 12 && a21 == 1 && a22 == 13 && a34 == 14 && a43 == 11 )
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 4://4. 28#已知3数挑战题来了
if (a11 == 16 && a12 == 14 && a21 == 2)
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 5: // 5. 31#已知3数挑战题又来了
if (a11 == 1 && a22 == 15 && a33 == 12)
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 6:// 6. 1# 已知4数原题
if (a21 == 5 && a22 == 10 && a23 == 11 && a24 == 8 )
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 7:// 7. 32#已知2数(15 14)高难度
if (a12 == 15 && a13 == 14)
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
case 8:// 8. 已知0数全部解7040
if (1)
{
cnt++;
sprintf_s(str, "%5d:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", cnt, i1, i2, i3, 34 - i1 - i2 - i3, i4, i5, i6, 34 - i4 - i5 - i6, i7, -34 + 2 * i1 + i2 + i3 + i4 - i6 + i7, 68 - 2 * i1 - i2 - i3 - i4 - i5 - i7, i5 + i6 - i7, 34 - i1 - i4 - i7, 68 - 2 * i1 - 2 * i2 - i3 - i4 - i5 + i6 - i7, -34 + 2 * i1 + i2 + i4 + i5 - i6 + i7, -34 + i1 + i2 + i3 + i4 + i7);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n%2d,%2d,%2d,%2d\n", a11, a12, a13, a14, a21, a22, a23, a24, a31, a32, a33, a34, a41, a42, a43, a44);
fwrite(str, 1, strlen(str), fp);
}
break;
}// switch
fflush(fp);
}
}
}
}
}
}
}
printf("%d\n", cnt);
printf("Elapsed time %0.2f\n", (clock() - t0) / 1000.0);
sprintf_s(str, "cnt= %d\n", cnt);
fwrite(str, 1, strlen(str), fp);
sprintf_s(str, "Elapsed time: %0.2f\n", (clock() - t0) / 1000.0);
fwrite(str, 1, strlen(str), fp);
fflush(fp);
}
fclose(fp);
return 0;
}
dlpg070
发表于 2020-2-14 16:01:41
本帖最后由 dlpg070 于 2020-2-14 16:22 编辑
dlpg070 发表于 2020-2-14 15:55
最近发现chyanog的解4阶幻方的c++代码,简单快速,适于解本主题各例题
我增加菜单,改变输入输出, ...
输出的例子
已知0---16个数字求全部4x4幻方,多个实例
=======
select= 7// 37#已知2数(15 14)高难度
1:1,15,14,4,5,11,8,10,12,6,9,7,16,2,3,13
1,15,14, 4
5,11, 8,10
12, 6, 9, 7
16, 2, 3,13
2:1,15,14,4,6,12,9,7,11,5,8,10,16,2,3,13
1,15,14, 4
6,12, 9, 7
11, 5, 8,10
16, 2, 3,13
3:1,15,14,4,7,9,6,12,10,8,11,5,16,2,3,13
1,15,14, 4
7, 9, 6,12
10, 8,11, 5
16, 2, 3,13
4:1,15,14,4,7,12,9,6,10,5,8,11,16,2,3,13
1,15,14, 4
7,12, 9, 6
10, 5, 8,11
16, 2, 3,13
5:1,15,14,4,8,10,11,5,12,6,7,9,13,3,2,16
1,15,14, 4
8,10,11, 5
12, 6, 7, 9
13, 3, 2,16
6:1,15,14,4,10,8,5,11,7,9,12,6,16,2,3,13
1,15,14, 4
10, 8, 5,11
7, 9,12, 6
16, 2, 3,13
7:1,15,14,4,10,11,8,5,7,6,9,12,16,2,3,13
1,15,14, 4
10,11, 8, 5
7, 6, 9,12
16, 2, 3,13
8:1,15,14,4,11,8,5,10,6,9,12,7,16,2,3,13
1,15,14, 4
11, 8, 5,10
6, 9,12, 7
16, 2, 3,13
9:1,15,14,4,12,6,7,9,8,10,11,5,13,3,2,16
1,15,14, 4
12, 6, 7, 9
8,10,11, 5
13, 3, 2,16
10:1,15,14,4,12,9,6,7,5,8,11,10,16,2,3,13
1,15,14, 4
12, 9, 6, 7
5, 8,11,10
16, 2, 3,13
11:2,15,14,3,5,10,7,12,11,8,9,6,16,1,4,13
2,15,14, 3
5,10, 7,12
11, 8, 9, 6
16, 1, 4,13
12:2,15,14,3,5,12,9,8,11,6,7,10,16,1,4,13
2,15,14, 3
5,12, 9, 8
11, 6, 7,10
16, 1, 4,13
13:2,15,14,3,8,9,12,5,11,6,7,10,13,4,1,16
2,15,14, 3
8, 9,12, 5
11, 6, 7,10
13, 4, 1,16
14:2,15,14,3,9,8,5,12,7,10,11,6,16,1,4,13
2,15,14, 3
9, 8, 5,12
7,10,11, 6
16, 1, 4,13
15:2,15,14,3,11,10,7,6,5,8,9,12,16,1,4,13
2,15,14, 3
11,10, 7, 6
5, 8, 9,12
16, 1, 4,13
16:2,15,14,3,12,5,8,9,7,10,11,6,13,4,1,16
2,15,14, 3
12, 5, 8, 9
7,10,11, 6
13, 4, 1,16
17:3,15,14,2,6,8,9,11,12,10,7,5,13,1,4,16
3,15,14, 2
6, 8, 9,11
12,10, 7, 5
13, 1, 4,16
18:3,15,14,2,6,10,11,7,9,5,8,12,16,4,1,13
3,15,14, 2
6,10,11, 7
9, 5, 8,12
16, 4, 1,13
19:3,15,14,2,6,10,11,7,12,8,5,9,13,1,4,16
3,15,14, 2
6,10,11, 7
12, 8, 5, 9
13, 1, 4,16
20:3,15,14,2,10,6,7,11,5,9,12,8,16,4,1,13
3,15,14, 2
10, 6, 7,11
5, 9,12, 8
16, 4, 1,13
21:3,15,14,2,10,6,7,11,8,12,9,5,13,1,4,16
3,15,14, 2
10, 6, 7,11
8,12, 9, 5
13, 1, 4,16
22:3,15,14,2,12,8,9,5,6,10,7,11,13,1,4,16
3,15,14, 2
12, 8, 9, 5
6,10, 7,11
13, 1, 4,16
23:4,15,14,1,5,8,11,10,12,9,6,7,13,2,3,16
4,15,14, 1
5, 8,11,10
12, 9, 6, 7
13, 2, 3,16
24:4,15,14,1,5,10,11,8,9,6,7,12,16,3,2,13
4,15,14, 1
5,10,11, 8
9, 6, 7,12
16, 3, 2,13
25:4,15,14,1,6,9,12,7,11,8,5,10,13,2,3,16
4,15,14, 1
6, 9,12, 7
11, 8, 5,10
13, 2, 3,16
26:4,15,14,1,7,6,9,12,10,11,8,5,13,2,3,16
4,15,14, 1
7, 6, 9,12
10,11, 8, 5
13, 2, 3,16
27:4,15,14,1,7,9,12,6,10,8,5,11,13,2,3,16
4,15,14, 1
7, 9,12, 6
10, 8, 5,11
13, 2, 3,16
28:4,15,14,1,9,6,7,12,5,10,11,8,16,3,2,13
4,15,14, 1
9, 6, 7,12
5,10,11, 8
16, 3, 2,13
29:4,15,14,1,10,5,8,11,7,12,9,6,13,2,3,16
4,15,14, 1
10, 5, 8,11
7,12, 9, 6
13, 2, 3,16
30:4,15,14,1,10,8,11,5,7,9,6,12,13,2,3,16
4,15,14, 1
10, 8,11, 5
7, 9, 6,12
13, 2, 3,16
31:4,15,14,1,11,5,8,10,6,12,9,7,13,2,3,16
4,15,14, 1
11, 5, 8,10
6,12, 9, 7
13, 2, 3,16
32:4,15,14,1,12,6,9,7,5,11,8,10,13,2,3,16
4,15,14, 1
12, 6, 9, 7
5,11, 8,10
13, 2, 3,16
cnt= 32
Elapsed time: 0.21
王守恩
发表于 2020-2-15 08:26:28
本帖最后由 王守恩 于 2020-2-15 08:36 编辑
王守恩 发表于 2020-2-13 11:16
能说下面的题是无解的吗?
+----+----+----+----+
能说下面的题是无解的吗?
+----+----+----+----+
| | | | |
+----+----+----+----+
| |1| 2| |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+