针对十以内的四个数,利用加减乘除四则运算和括号,快速算出结果为24.每个数字仅可使用一次,不能重复使用。当有多种运算方案时,屏幕显示所有方案;当无解时,给出无解提示;能够检测错误的无效输入,并给出提示!
递归
a b c d
a + ( b + ( c + d ) ) //第一种算式
- - -
* * *
/ / /
( a + b ) + ( c + d ) // 第二种算式
- - -
* * *
/ / /
递归不会用 最笨的算法吧 好多事得自己做
一个最累的程序 写了半天 还不是很完善 你参考一下吧
#include <stdio.h>
#define Ad(x,y) (x+y)
#define Mi(x,y) (x-y)
#define Mu(x,y) (x*y)
#define Di(x,y) (x/y)
int falg=0;
float po[24][4];
void game24();
void test(float a,float b,float c,float d);
void write(float (*po)[4] ,float a,float b,float c,float d);
int main()
{
game24();
return 0;
}
void test(float a,float b,float c,float d)
{
//a+(b+c+d)
if(Ad(a,Ad(b,Ad(c,d)))==24){ printf(" %2.0f+%2.0f+%2.0f+%2.0f=24 \n",a,b,c,d);falg=1;}
if(Ad(a,Ad(b,Mi(c,d)))==24){ printf(" %2.0f+%2.0f+%2.0f-%2.0f \n",a,b,c,d);falg=1;}
if(Ad(a,Ad(b,Mu(c,d)))==24){ printf(" %2.0f+%2.0f+%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Ad(a,Ad(b,Di(c,d)))==24){ printf(" %2.0f+%2.0f+%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Ad(a,Mi(b,Ad(c,d)))==24){ printf(" %2.0f+%2.0f-%2.0f+%2.0f \n",a,b,c,d);falg=1;}
if(Ad(a,Mi(b,Mi(c,d)))==24){ printf(" %2.0f+%2.0f-%2.0f%2.0f \n",a,b,c,d);falg=1;}
if(Ad(a,Mi(b,Mu(c,d)))==24){ printf(" %2.0f+%2.0f-%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Ad(a,Mi(b,Di(c,d)))==24){ printf(" %2.0f+%2.0f-%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Ad(a,Mu(b,Ad(c,d)))==24){ printf(" %2.0f+%2.0f*(%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Ad(a,Mu(b,Mi(c,d)))==24){ printf(" %2.0f+%2.0f*(%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Ad(a,Mu(b,Mu(c,d)))==24){ printf(" %2.0f+%2.0f*%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Ad(a,Mu(b,Di(c,d)))==24){ printf(" %2.0f+%2.0f*%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Ad(c,d)!=0&&Ad(a,Di(b,Ad(c,d)))==24){ printf(" %2.0f+%2.0f/(%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Mi(c,d)!=0&&Ad(a,Di(b,Mi(c,d)))==24){ printf(" %2.0f+%2.0f/(%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(c,d)!=0&&Ad(a,Di(b,Mu(c,d)))==24){ printf(" %2.0f+%2.0f/%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Di(c,d)!=0&&Ad(a,Di(b,Di(c,d)))==24){ printf(" %2.0f+%2.0f/%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Ad(b,Ad(c,d)))==24){ printf(" %2.0f-%2.0f+%2.0f+%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Ad(b,Mi(c,d)))==24){ printf(" %2.0f-%2.0f+%2.0f-%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Ad(b,Mu(c,d)))==24){ printf(" %2.0f-%2.0f+%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Mi(a,Ad(b,Di(c,d)))==24){ printf(" %2.0f-%2.0f+%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Mi(b,Ad(c,d)))==24){ printf(" %2.0f-%2.0f-%2.0f+%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Mi(b,Mi(c,d)))==24){ printf(" %2.0f-%2.0f-%2.0f-%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Mi(b,Mu(c,d)))==24){ printf(" %2.0f-%2.0f-%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Mi(a,Mi(b,Di(c,d)))==24){ printf(" %2.0f-%2.0f-%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Mi(a,Mu(b,Ad(c,d)))==24){ printf(" %2.0f-%2.0f*(%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Mi(a,Mu(b,Mi(c,d)))==24){ printf(" %2.0f-%2.0f*(%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Mi(a,Mu(b,Mu(c,d)))==24){ printf(" %2.0f-%2.0f*%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Mi(a,Mu(b,Di(c,d)))==24){ printf(" %2.0f-%2.0f*%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Ad(c,d)!=0&&Mi(a,Di(b,Ad(c,d)))==24){ printf(" %2.0f-%2.0f/(%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Mi(c,d)!=0&&Mi(a,Di(b,Mi(c,d)))==24){ printf(" %2.0f-%2.0f/(%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(c,d)!=0&&Mi(a,Di(b,Mu(c,d)))==24){ printf(" %2.0f-%2.0f/%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Di(c,d)&&Mi(a,Di(b,Di(c,d)))==24){ printf(" %2.0f-%2.0f/%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Mu(a,Ad(b,Ad(c,d)))==24){ printf(" %2.0f*(%2.0f+%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(a,Ad(b,Mi(c,d)))==24){ printf(" %2.0f*(%2.0f+%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(a,Ad(b,Mu(c,d)))==24){ printf(" %2.0f*(%2.0f+%2.0f*%2.0f) \n",a,b,c,d);falg=1;}
if(d!=0&&Mu(a,Ad(b,Di(c,d)))==24){ printf(" %2.0f*(%2.0f+%2.0f/%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(a,Mi(b,Ad(c,d)))==24){ printf(" %2.0f*(%2.0f-%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(a,Mi(b,Mi(c,d)))==24){ printf(" %2.0f*(%2.0f-%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(a,Mi(b,Mu(c,d)))==24){ printf(" %2.0f*(%2.0f-%2.0f*%2.0f) \n",a,b,c,d);falg=1;}
if(d!=0&&Mu(a,Mi(b,Di(c,d)))==24){ printf(" %2.0f*(%2.0f-%2.0f/%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(a,Mu(b,Ad(c,d)))==24){ printf(" %2.0f*(%2.0f*(%2.0f+%2.0f)) \n",a,b,c,d);falg=1;}
if(Mu(a,Mu(b,Mi(c,d)))==24){ printf(" %2.0f*(%2.0f*(%2.0f-%2.0f)) \n",a,b,c,d);falg=1;}
if(Mu(a,Mu(b,Mu(c,d)))==24){ printf(" %2.0f*%2.0f*%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Mu(a,Mu(b,Di(c,d)))==24){ printf(" %2.0f*%2.0f*%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Ad(c,d)!=0&&Mu(a,Di(b,Ad(c,d)))==24){ printf(" %2.0f*(%2.0f/(%2.0f+%2.0f)) \n",a,b,c,d);falg=1;}
if(Mi(c,d)!=0&&Mu(a,Di(b,Mi(c,d)))==24){ printf(" %2.0f*(%2.0f/(%2.0f-%2.0f)) \n",a,b,c,d);falg=1;}
if(Mu(c,d)!=0&&Mu(a,Di(b,Mu(c,d)))==24){ printf(" %2.0f*%2.0f/%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Di(c,d)!=0&&Mu(a,Di(b,Di(c,d)))==24){ printf(" %2.0f*%2.0f/%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Ad(b,Ad(c,d))!=0&&Di(a,Ad(b,Ad(c,d)))==24){ printf(" %2.0f/%2.0f+%2.0f+%2.0f \n",a,b,c,d);falg=1;}
if(Ad(b,Mi(c,d))!=0&&Di(a,Ad(b,Mi(c,d)))==24){ printf(" %2.0f/%2.0f+%2.0f-%2.0f \n",a,b,c,d);falg=1;}
if(Ad(b,Mu(c,d))!=0&&Di(a,Ad(b,Mu(c,d)))==24){ printf(" %2.0f/%2.0f+%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Ad(b,Di(c,d))!=0&&Di(a,Ad(b,Di(c,d)))==24){ printf(" %2.0f/%2.0f+%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Mi(b,Ad(c,d))!=0&&Di(a,Mi(b,Ad(c,d)))==24){ printf(" %2.0f/%2.0f-%2.0f+%2.0f \n",a,b,c,d);falg=1;}
if(Mi(b,Mi(c,d))!=0&&Di(a,Mi(b,Mi(c,d)))==24){ printf(" %2.0f/%2.0f-%2.0f-%2.0f \n",a,b,c,d);falg=1;}
if(Mi(b,Mu(c,d))!=0&&Di(a,Mi(b,Mu(c,d)))==24){ printf(" %2.0f/%2.0f-%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Mi(b,Di(c,d))!=0&&Di(a,Mi(b,Di(c,d)))==24){ printf(" %2.0f/%2.0f-%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Mu(b,Ad(c,d))!=0&&Di(a,Mu(b,Ad(c,d)))==24){ printf(" %2.0f/%2.0f*(%2.0f+%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(b,Mi(c,d))!=0&&Di(a,Mu(b,Mi(c,d)))==24){ printf(" %2.0f/%2.0f*(%2.0f-%2.0f) \n",a,b,c,d);falg=1;}
if(Mu(b,Mu(c,d))!=0&&Di(a,Mu(b,Mu(c,d)))==24){ printf(" %2.0f/%2.0f*%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Mu(b,Di(c,d))!=0&&Di(a,Mu(b,Di(c,d)))==24){ printf(" %2.0f/%2.0f*%2.0f/%2.0f \n",a,b,c,d);falg=1;}
if(Di(b,Ad(c,d))!=0&&Di(a,Di(b,Ad(c,d)))==24){ printf(" %2.0f/%2.0f/(%2.0f%2.0f) \n",a,b,c,d);falg=1;}
if(Di(b,Mi(c,d))!=0&&Di(a,Di(b,Mi(c,d)))==24){ printf(" %2.0f%2.0f/(%2.0f%2.0f) \n",a,b,c,d);falg=1;}
if(Di(b,Mu(c,d))!=0&&Di(a,Di(b,Mu(c,d)))==24){ printf(" %2.0f/%2.0f%2.0f*%2.0f \n",a,b,c,d);falg=1;}
if(d!=0&&Di(b,Di(c,d))!=0&&Di(a,Di(b,Di(c,d)))==24){ printf(" %2.0f/%2.0f/%2.0f/%2.0f \n",a,b,c,d);falg=1;}
}
void game24()
{
float a=0,b=0,c=0,d=0;
while(1)
{
puts("请输入a b c d 四个数 1~9");
scanf("%f %f %f %f",&a,&b,&c,&d);
printf("\n你输入的是: %3.0f%3.0f%3.0f%3.0f\n",a,b,c,d);
if((a<10&&a>0)&&(b<10&&b>0)&&(c<10&&c>0)&&(d<10&&d>0)) break;
}
test(a,b,c,d);
test(a,b,d,c);
test(a,c,b,d);
test(a,c,d,b);
test(a,d,b,c);
test(a,d,c,b);
test(b,a,b,c);
test(b,a,c,b);
test(b,c,a,d);
test(b,c,d,a);
test(b,d,a,c);
test(b,d,c,a);
test(c,a,b,c);
test(c,a,c,b);
test(c,b,c,d);
test(c,b,d,c);
test(c,d,a,b);
test(c,d,b,a);
test(d,a,b,c);
test(d,a,c,b);
test(d,b,c,d);
test(d,b,d,c);
test(d,c,a,b);
test(d,c,b,a);
if(falg==0) puts("无解");
}
能不能说的详细一点
追答四个for 循环,在从for循环后继续检查是否符合条件,符合就printf
追问好吧,我再看看,谢了!
追答没事!
计算机智能算数的C语言程序
if(Ad(c,d)!=0&&Ad(a,Di(b,Ad(c,d)))==24){ printf(" %2.0f+%2.0f\/(%2.0f+%2.0f) \\n",a,b,c,d);falg=1;} if(Mi(c,d)!=0&&Ad(a,Di(b,Mi(c,d)))==24){ printf(" %2.0f+%2.0f\/(%2.0f-%2.0f) \\n",a,b,c,d);falg=1;} if(M...
...十位和个位,并且计算十位百位个位的和。”c语言程序
{ int a, b, c, d, e; printf("输入一个三位数:"); scanf("%d", &a); if (a > 999 || a < 100) printf("输入错误!"); else { b = a \/ 100; c = a \/ 10 % 10; d = a % 100 % 10; e = b + c + d; printf ("百位是%d,十位是%d,个位是%d,各个位数的和为%d", b...
c语言属于什么类型的语言
c语言属于计算机程序设计语言。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。尽管C语言提供了许多低级处理的功能,但仍然保持着跨平台的特性。计算机(computer)俗称电脑,是一种用于高速计算的电子计算机器,可以进行数值计算,又可以进行逻辑计算,还具有存储记忆功能。
用C语言编写AT89C51单片机程序,设计一个智能数字钟。
这是我的课程设计,6个数码管,显示时分秒,三个按钮P0.0、P0.1、P0.2可以调时,有8个彩灯(可以不要),没闹钟、倒计时之类的。呵呵。include<AT89X52.H> define uchar unsigned char int i;\/\/***时钟的 uchar one[10]={0xbf,0xb0,0xdb,0xcf,0xe6,0xed,0xfd,0x07,0xff,0xef}...
c语言有什么用 c语言使用流程
C语言凭借其强大的硬件访问能力,为开发者提供了便捷的工具。综上所述,C语言不仅是计算机技术的基础,也是推动信息技术发展的重要力量。掌握C语言,意味着你掌握了一把打开软件开发世界大门的钥匙,可以涉足操作系统、底层软件、嵌入式系统、硬件交互等多个领域,开启无限的编程可能性。
智能传感系统中c语言程序用什么软件写
可以使用microsoft visual c++。C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。
计算机程序设计语言有哪些
C++ C++在中国大陆程序员的圈子里通常读作“Cplusplus”,而在西方程序员通常读作“Cplus plus”和“CPP”。它是一种广泛使用的计算机编程语言。c是一种通用的程序设计语言,具有静态数据类型检查和多种编程范式。支持过程化程序设计、数据抽象、面向对象程序设计、通用程序设计等多种程序设计风格。C语言、...
编程有什么编程语言
可用C、C++进行程序扩展。Python是一种面向对象的解释型计算机程序设计语言,由Guido van Rossum发明。Python语言简洁清晰,语法简洁,易学易用,速度快,免费,是一种高层语言。Python具有可移植性、解释性、面向对象、可扩展性等特性,广泛应用于web开发、科学计算、数据分析、人工智能、自动化脚本等领域。
C语言能做人工智能吗?
可以做。无论是何种软件,其本质都是二进制语言,即供机器识别的机器代码。而C语言与可以与任意的机器语言语句相对应,也就是任何二进制语言,均可以由C语言实现。从这个角度来说,C语言可以实现一切功能或软件,同样包括人工智能。实际上,由于人工智能的运算量很大,在执行的时候需要很高的执行效率,目前...
c语言程序设计学什么
C语言程序设计课程是入门级的程序设计课程,针对没有或很少具有编程经验的在职人员。课程通过学习C语言编程的基础语法,对程序设计有一个基本的认识,为后续计算机专业课程以及面向对象程序设计课程的学习打下基础。随着科技的发展和人工智能时代的来临,计算机中程序设计具有越来越重要的地位。C语言程序设计是...