#include "stdio.h"
void main()
{
void exchange(int e, float &a, float &b, float &c, float &d);
float operate(int o, int i, int j, int k, float a, float b, float c, float d);
int same_all(int i1, int j1, int k1, int i2, int j2, int k2);
void sort(int i, int j, int k, int &mix, int &mid, int &max);
void print(int o, int i, int j, int k, float a, float b, float c, float d);
int e1, e2, e3, i[64], j[64], k[64], m, n, flag1, flag2, flag;
float a, b, c, d;
int A, B, C, D;
float ope24;
printf("【二十四点】\n输入四个小于14的正整数,\n得出此四个数经过加减乘除得到24的过程\n");
while (1)
{
n = 1;
flag1 = 0;
printf("\n请输入:");
scanf_s("%f%f%f%f", &a, &b, &c, &d);
A = (int)a; B = (int)b; C = (int)c; D = (int)d;
if (a < 1 || a > 13 || b < 1 || b > 13 || c < 1 || c > 13 || d < 1 || d > 13)
{
printf("输入不合法!请重新输入!\n");
continue;
}
else if (a - A > 0 || b - B > 0 || c - C > 0 || d - D > 0)
{
printf("输入不合法!请重新输入!\n");
continue;
}
else
{
for (e1 = 1; e1 >= 0; e1--)
{
if (e1 == 0)
{
exchange(e1, a, b, c, d);
}
for (e2 = 1; e2 <= 4; e2++)
{
for (e3 = 1; e3 <= 3; e3++)
{
exchange(e3, a, b, c, d);
for (i[0] = 1; i[0] <= 4; i[0]++)
{
for (j[0] = 1; j[0] <= 4; j[0]++)
{
for (k[0] = 1; k[0] <= 4; k[0]++)
{
flag2 = 0;
for (flag = 1; flag <= 5; flag++)
{
ope24 = operate(flag, i[0], j[0], k[0], a, b, c, d);
if ((ope24 - 24 >= 0 && ope24 - 24 < 0.001) || (24 - ope24 >= 0 && 24 - ope24 < 0.001))
{
sort(i[0], j[0], k[0], i[63], j[63], k[63]);
for (m = 1; m < n; m++)
{
if (same_all(i[63], j[63], k[63], i[m], j[m], k[m]))
{
flag2 = 1;
break;
}
}
if (flag2 == 1) continue;
else
{
sort(i[0], j[0], k[0], i[n], j[n], k[n]);
n++;
print(flag, i[0], j[0], k[0], a, b, c, d);
flag1 = 1;
}
}
}
}
}
}
}
}
}
if (flag1 == 0)
{
printf("\n无解!\n");
}
}
}
}
void exchange(int e, float &a, float &b, float &c, float &d)
{
float temp;
switch (e)
{
case 0:
{
temp = a;
a = d;
d = temp;
temp = b;
b = c;
c = temp;
}break;
case 1:
{
temp = a;
a = b;
b = temp;
}break;
case 2:
{
temp = a;
a = c;
c = temp;
}break;
case 3:
{
temp = a;
a = d;
d = temp;
}break;
}
}
float operate(int o, int i, int j, int k, float a, float b, float c, float d)
{
float ope(int o, float x, float y);
switch (o)
{
case 1:return ope(k, ope(j, ope(i, a, b), c), d); break;
case 2:return ope(j, ope(i, a, b), ope(k, c, d)); break;
case 3:return ope(k, ope(i, a, ope(j, b, c)), d); break;
case 4:return ope(i, a, ope(k, ope(j, b, c), d)); break;
case 5:return ope(i, a, ope(j, b, ope(k, c, d))); break;
}
}
float ope(int o, float x, float y)
{
switch (o)
{
case 1:return(x + y);
case 2:
if (x - y < 0) return 666;
else return(x - y);
case 3:return(x * y);
case 4:
if (y != 0) return(x / y);
else return 0;
}
}
int same_all(int i1, int j1, int k1, int i2, int j2, int k2)
{
int same(int i1, int j1, int k1, int i2, int j2, int k2);
void swap(int &x, int &y);
int s;
if (i1 == i2&&j1 == j2&&k1 == k2) return 1;
for (s = 1; s <= 2; s++)
{
if (same(i1, j1, k1, i2, j2, k2)) return 1;
swap(i1, j1); swap(i2, j2);
if (same(i1, j1, k1, i2, j2, k2)) return 1;
swap(i1, k1); swap(i2, k2);
if (same(i1, j1, k1, i2, j2, k2)) return 1;
swap(j1, k1); swap(j2, k2);
}
return 0;
}
int same(int i1, int j1, int k1, int i2, int j2, int k2)
{
if (i1 == i2&&j1 == 2 && j2 == 2 && (k1 == 1 || k2 == 1) && k1 + k2 == 3) return 1;
else if (i1 == i2&&j1 == 4 && j2 == 4 && k1 + k2 == 7) return 1;
else return 0;
}
void sort(int i, int j, int k, int &mix, int &mid, int &max)
{
void swap(int &x, int &y);
if (i > j)
{
swap(i, j);
}
if (i > k)
{
swap(i, k);
}
if (j > k)
{
swap(j, k);
}
mix = i;
mid = j;
max = k;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void print(int p, int i, int j, int k, float a, float b, float c, float d)
{
void print1(int p1, int p2, int i, int j, int k, float a, float b, float c, float d);
void print2(int p1, int p2, int i, int j, int k, float a, float b, float c, float d);
void print3(int p1, int p2, int i, int j, int k, float a, float b, float c, float d);
void print4(int p1, int p2, int i, int j, int k, float a, float b, float c, float d);
void print5(int p1, int p2, int i, int j, int k, float a, float b, float c, float d);
int flag1 = 0, flag2 = 0;
switch (p)
{
case 1:
{
if (i <= 2 && j > 2) flag1 = 1;
if (j <= 2 && k > 2) flag2 = 1;
print1(flag1, flag2, i, j, k, a, b, c, d);
}break;
case 2:
{
if (i <= 2 && j > 2) flag1 = 1;
if (j == 2 && k <= 2) flag2 = 1;
if (j == 4 && k > 2) flag2 = 1;
if (j > 2 && k <= 2) flag2 = 1;
print2(flag1, flag2, i, j, k, a, b, c, d);
}break;
case 3:
{
if (i <= 2) flag1 = 1;
if (i == 2 && j <= 2) flag2 = 1;
if (i == 4 && j > 2) flag2 = 1;
if (i > 2 && j <= 2) flag2 = 1;
print3(flag1, flag2, i, j, k, a, b, c, d);
}break;
case 4:
{
if (j <= 2 && k > 2) flag1 = 1;
if (i == 2 && k <= 2) flag2 = 1;
if (i == 4 && k > 2) flag2 = 1;
if (i > 2 && k <= 2) flag2 = 1;
print4(flag1, flag2, i, j, k, a, b, c, d);
}break;
case 5:
{
if (i == 2 && j <= 2) flag1 = 1;
if (i == 4 && j > 2) flag1 = 1;
if (i > 2 && j <= 2) flag1 = 1;
if (j == 2 && k <= 2) flag2 = 1;
if (j == 4 && k > 2) flag2 = 1;
if (j > 2 && k <= 2) flag2 = 1;
print5(flag1, flag2, i, j, k, a, b, c, d);
}break;
}
}
void print1(int p1, int p2, int i, int j, int k, float a, float b, float c, float d)
{
void printmark(int p);
printf("\n");
if (p1 == 1) printf("(");
if (p2 == 1) printf("(");
printf("%1.0f", a);
printmark(i);
printf("%1.0f", b);
if (p1 == 1) printf(")");
printmark(j);
printf("%1.0f", c);
if (p2 == 1) printf(")");
printmark(k);
printf("%1.0f", d);
printf("=24\n");
}
void print2(int p1, int p2, int i, int j, int k, float a, float b, float c, float d)
{
void printmark(int p);
printf("\n");
if (p1 == 1) printf("(");
printf("%1.0f", a);
printmark(i);
printf("%1.0f", b);
if (p1 == 1) printf(")");
printmark(j);
if (p2 == 1) printf("(");
printf("%1.0f", c);
printmark(k);
printf("%1.0f", d);
if (p2 == 1) printf(")");
printf("=24\n");
}
void print3(int p1, int p2, int i, int j, int k, float a, float b, float c, float d)
{
void printmark(int p);
printf("\n");
if (p1 == 1) printf("(");
printf("%1.0f", a);
printmark(i);
if (p2 == 1) printf("(");
printf("%1.0f", b);
printmark(j);
printf("%1.0f", c);
if (p1 == 1) printf(")");
if (p2 == 1) printf(")");
printmark(k);
printf("%1.0f", d);
printf("=24\n");
}
void print4(int p1, int p2, int i, int j, int k, float a, float b, float c, float d)
{
void printmark(int p);
printf("\n");
printf("%1.0f", a);
printmark(i);
if (p1 == 1) printf("(");
if (p2 == 1) printf("(");
printf("%1.0f", b);
printmark(j);
printf("%1.0f", c);
if (p1 == 1) printf(")");
printmark(k);
printf("%1.0f", d);
if (p2 == 1) printf(")");
printf("=24\n");
}
void print5(int p1, int p2, int i, int j, int k, float a, float b, float c, float d)
{
void printmark(int p);
printf("\n");
printf("%1.0f", a);
printmark(i);
if (p1 == 1) printf("(");
printf("%1.0f", b);
printmark(j);
if (p2 == 1) printf("(");
printf("%1.0f", c);
printmark(k);
printf("%1.0f", d);
if (p1 == 1) printf(")");
小波思基
- 粉丝: 87
- 资源: 1万+
最新资源
- 基于知识图谱的推荐模型详细文档+全部资料+优秀项目.zip
- 基于知识图谱的书籍推荐的问答系统及可视化详细文档+全部资料+优秀项目.zip
- 基于知识图谱的心血管疾病问答系统详细文档+全部资料+优秀项目.zip
- 基于知识图谱的中药方剂可视化及问答系统详细文档+全部资料+优秀项目.zip
- 基于知识图谱技术优化漏洞检索-最初版本详细文档+全部资料+优秀项目.zip
- 机械设计SCARA机器人贴标机sw18可编辑全套设计资料100%好用.zip
- 机械设计SIM卡组装自动机sw18可编辑全套设计资料100%好用.zip
- 机械设计SATA7+6PIN自动检测包装机sw18可编辑全套设计资料100%好用.zip
- 机械设计W1300X L2000X H700动力辊道输送机sw20可编辑全套设计资料100%好用.zip
- 基于 vue + python + 树莓派的媒体播放器全部资料+详细文档+高分项目.zip
- 毕业设计,基于树莓派的远程温度监控系统设计全部资料+详细文档+高分项目.zip
- 基于 树莓派 的项目,天气实况、天气预报,实时温度、湿度、空气污染指数,自带中文语音播报,根据思科 EA 系列路由器,实现自动门禁功能。全部资料+详细文档+高分项目.zip
- 基于FLASK与opencv树莓派版本的简单直播系统,全部资料+详细文档+高分项目.zip
- 基于GPPO算法的树莓派小车路径规划全部资料+详细文档+高分项目.zip
- 基于Java编写的基于树莓派的电子墨水屏相册全部资料+详细文档+高分项目.zip
- 基于Picoprobe的树莓派pico调试器全部资料+详细文档+高分项目.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈