using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MathCalc { class Program { static void Main(string[] args) { /* Sub main() '开始计算 getall 100 getall 100, False End Sub Private Sub getall(ByVal theresult As Long, Optional ByVal asc As Boolean = True) Dim temp As Long '四进制转换中间变量 Dim x(8) As String '获得8个位置四则运算符 Dim op(5) As String '定义四个四则运算符 Dim i As Long, j As Long '循环变量 Dim out As String '最终表达式 Dim num As Long '循环范围和输出计数 op(0) = "+" op(1) = "-" op(2) = "*" op(3) = "/" op(4) = "" For i = 0 To 5 ^ 8 - 1 temp = i x(0) = IIf(asc, 1, 9) For j = 1 To 8 x(j) = op(temp Mod 5) & IIf(asc, j + 1, 9 - j) temp = temp / 5 Next out = Join(x, "") If Evaluate(out) = theresult Then '条件判断 num = num + 1 Debug.Print "解 " & num & ":" & vbTab & Replace(out, ".0", "") & "=" & theresult End If Next If num = 0 Then Debug.Print "无解!" If num > 0 Then Debug.Print "共 " & num & " 组解!" End Sub 本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/northwolves/archive/2009/11/20/4841068.aspx*/ Console.WriteLine("符合条件的解:"); int count=getall(100); Console.WriteLine("总共{0}组解", count); } static int getall(int result) { long temp; int count = 0; string[] x = new string[9]; string[] op ={ "+", "-", "*", "/", "" }; for (int i = 0; i < Math.Pow(5, 8) - 1; i++) { temp = i; x[0] = "1"; for (int j = 1; j <=8; j++) { x[j] = op[temp % 5] + (j + 1); temp /= 5; } string sout = ""; for (int j = 0; j < x.Length; j++) { sout += x[j]; } if (Evaluate(sout) == result) { count++; Console.WriteLine(sout+" = "+result); } } return count; } static double Evaluate(string x) { char [] charx=x.ToCharArray(); double []num=new double [9]; char [] Op=new char[8]; int numc=0,opc=0,indx=0; for (int i=0;i<charx.Length;i++) { if(charx[i]=='+'||charx[i]=='-'||charx[i]=='*'||charx[i]=='/') { Op[opc++]=charx[i];//当前符号,前面是数字,后面是剩余串 num[numc++]=CharArrayToInt(charx,indx,i-1);//从inx开始到i-1都是数字 indx=i+1;//下一个数字开始下标 } } num[numc++] = CharArrayToInt(charx, indx, charx.Length-1);//最后一个数 // double result = 0; //opc==numc-1,永远成立 Stack<double> StNum = new Stack<double>(); Stack<char> StOp = new Stack<char>(); int stnumindx = 0;//num 中下一个坐标 StNum.Push(num[stnumindx++]); for (int i = 0; i < opc; ) { if (Op[i] == '+' || Op[i] == '-')//加减操作 { if (StOp.Count == 0) { StNum.Push(num[stnumindx++]);//压两个数 StOp.Push(Op[i++]);//压一个操作符 } else//符号里有符号,必然不可能是乘除,当前符号不懂 { double n1 = StNum.Pop();//第二个数 double n2 = StNum.Pop(); switch (StOp.Pop()) { case '+': StNum.Push(n1 + n2); break; case '-': StNum.Push(n2 - n1); break; } } } else//乘除就直接算 { double n1 = StNum.Pop(); double n2 = num[stnumindx++]; switch (Op[i++]) { case '*': StNum.Push(n1 * n2); break; case '/': StNum.Push(n1 / n2); break; } } }//算完后,StNum中就是结果 //最后一次计算 if (StOp.Count != 0)//里面还有数据,必然是+,- { double nn1 = StNum.Pop();//第二个数 double nn2 = StNum.Pop(); switch (StOp.Pop()) { case '+': StNum.Push(nn1 + nn2); break; case '-': StNum.Push(nn2 - nn1); break; } } return StNum.Pop(); } static double CharArrayToInt(char[] data) { string s = ""; foreach (char d in data) { s += d; } return double.Parse(s); } static double CharArrayToInt(char[] data, int start, int end) { string s = ""; for(int i=start;i<=end;i++) { s += data[i]; } return double.Parse(s); } } }