假设四个整数a,b,c,d,如果不考虑括号优先级的情况,仅用四则运算将他们连接
a+b-c*d,共有4*4*4=64种,如果考虑加括号的情况共有5种情况,总计5*64=320
种表达式。
class Program
{
static void Main(string[] args)
{
int i, j, k, t;
System.Console.WriteLine("please input four integer(1-10)");
i = Int32.Parse(System.Console.ReadLine());
j = Int32.Parse(System.Console.ReadLine());
k = Int32.Parse(System.Console.ReadLine());
t = Int32.Parse(System.Console.ReadLine());
if (Get24(i, j, k, t)) ;
else
System.Console.WriteLine("sorry,the four values can not calculate to 24!");
System.Console.ReadLine();
}
{
static void Main(string[] args)
{
int i, j, k, t;
System.Console.WriteLine("please input four integer(1-10)");
i = Int32.Parse(System.Console.ReadLine());
j = Int32.Parse(System.Console.ReadLine());
k = Int32.Parse(System.Console.ReadLine());
t = Int32.Parse(System.Console.ReadLine());
if (Get24(i, j, k, t)) ;
else
System.Console.WriteLine("sorry,the four values can not calculate to 24!");
System.Console.ReadLine();
}
//计算两个数运算的结果
static float Calculate(float x, float y, int op)
{
switch (op)
{
case 1: return x + y;
case 2: return x - y;
case 3: return x * y;
case 4: return x / y;
}
//return 0;
}
static float Calculate(float x, float y, int op)
{
switch (op)
{
case 1: return x + y;
case 2: return x - y;
case 3: return x * y;
case 4: return x / y;
}
//return 0;
}
//计算类型如((A op1 B)op2 C)op3 D
static float Calculate_Modle1(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num1, num2, op1);
result2 = Calculate(result1, num3, op2);
result3 = Calculate(result2, num4, op3);
return result3;
}
//计算类型如(A op1 B)op2 (C op3 D)
static float Calculate_Modle2(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num1, num2, op1);
result2 = Calculate(num3, num4, op3);
result3 = Calculate(result1, result2, op2);
return result2;
}
//计算类型如A op1 ((B op2 C) op3 D)
static float Calculate_Modle3(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num2, num3, op2);
result2 = Calculate(result1, num4, op3);
result3 = Calculate(num1, result2, op1);
return result3;
}
//计算类型如A op1 (B op2( C op3 D))
static float Calculate_Modle4(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num3, num4, op3);
result2 = Calculate(num2, result1, op2);
result3 = Calculate(num1, result2, op1);
return result3;
}
//计算类型如(A op1 (B op2 C)) op3 D
static float Calculate_Modle5(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num2, num3, op2);
result2 = Calculate(num1, result1, op1);
result3 = Calculate(result2, num4, op3);
return result3;
}
//计算24(四个数通过加减乘除四则运算)
static bool Get24(int num1, int num2, int num3, int num4)
{
bool flag = false;
char []op={'#','+','-','*','/'};
for(int op1=1;op1<=4;op1++)
for(int op2=1;op2<=4;op2++)
for (int op3 = 1; op3 <= 4; op3++)
{
if (Calculate_Modle1(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("(({0}{1}{2}){3}{4}){5}{6}=24", num1, op[op1],num2,op[op2], num3,op[op3], num4 );
flag = true;
}
if (Calculate_Modle2(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("({0}{1}{2}){3}({4}{5}{6})=24", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
static float Calculate_Modle1(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num1, num2, op1);
result2 = Calculate(result1, num3, op2);
result3 = Calculate(result2, num4, op3);
return result3;
}
//计算类型如(A op1 B)op2 (C op3 D)
static float Calculate_Modle2(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num1, num2, op1);
result2 = Calculate(num3, num4, op3);
result3 = Calculate(result1, result2, op2);
return result2;
}
//计算类型如A op1 ((B op2 C) op3 D)
static float Calculate_Modle3(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num2, num3, op2);
result2 = Calculate(result1, num4, op3);
result3 = Calculate(num1, result2, op1);
return result3;
}
//计算类型如A op1 (B op2( C op3 D))
static float Calculate_Modle4(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num3, num4, op3);
result2 = Calculate(num2, result1, op2);
result3 = Calculate(num1, result2, op1);
return result3;
}
//计算类型如(A op1 (B op2 C)) op3 D
static float Calculate_Modle5(float num1, float num2, float num3, float num4, int op1, int op2, int op3)
{
float result1, result2, result3;
result1 = Calculate(num2, num3, op2);
result2 = Calculate(num1, result1, op1);
result3 = Calculate(result2, num4, op3);
return result3;
}
//计算24(四个数通过加减乘除四则运算)
static bool Get24(int num1, int num2, int num3, int num4)
{
bool flag = false;
char []op={'#','+','-','*','/'};
for(int op1=1;op1<=4;op1++)
for(int op2=1;op2<=4;op2++)
for (int op3 = 1; op3 <= 4; op3++)
{
if (Calculate_Modle1(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("(({0}{1}{2}){3}{4}){5}{6}=24", num1, op[op1],num2,op[op2], num3,op[op3], num4 );
flag = true;
}
if (Calculate_Modle2(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("({0}{1}{2}){3}({4}{5}{6})=24", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
}
if (Calculate_Modle3(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("{0}{1}(({2}{3}{4}){5}{6})", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
if (Calculate_Modle3(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("{0}{1}(({2}{3}{4}){5}{6})", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
}
if (Calculate_Modle4(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("{0}{1}({2}{3}({4}{5}{6}))=24", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
if (Calculate_Modle4(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("{0}{1}({2}{3}({4}{5}{6}))=24", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
}
if (Calculate_Modle5(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("({0}{1}({2}{3}{4})){5}{6}=24", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
if (Calculate_Modle5(num1, num2, num3, num4, op1, op2, op3) == 24)
{
System.Console.WriteLine("({0}{1}({2}{3}{4})){5}{6}=24", num1, op[op1], num2, op[op2], num3, op[op3], num4);
flag = true;
}
}
return flag;
}
return flag;
}
}
转载于:https://blog.51cto.com/cs2011/515253