这个其实可以非常复杂的,因为有可能有多个解,且有重复的解,所以编写方法有多种,以下是最简单的一种#include <stdio.h>#include <string.h>#include <math.h>#define PRECISION 1E-6#define COUNT_OF_NUMBER 4#define NUMBER_TO_BE_CAL 24double number[COUNT_OF_NUMBER];char expression[COUNT_OF_NUMBER][100];int Search(int n){ int i,j; double a, b; char expa[100], expb[100]; if (n == 1) { if( fabs(number[0] - NUMBER_TO_BE_CAL) < PRECISION ) { printf("%s\n", expression[0] ); return 1; } else { return 0; } } for ( i = 0; i < n; i++) { for( j = i + 1; j < n; j++) { a = number[i]; b = number[j]; number[j] = number[n - 1]; strcpy(expa , expression[i]); strcpy( expb , expression[j]); strcpy( expression[j] , expression[n - 1]); sprintf(expression[i],"(%s+%s)",expa , expb); number[i] = a + b; if ( Search(n - 1) ) return 1; sprintf(expression[i],"(%s-%s)",expa , expb); number[i] = a - b; if ( Search(n - 1) ) return 1; sprintf(expression[i],"(%s-%s)",expb , expa); number[i] = b - a; if ( Search(n - 1) ) return 1; sprintf(expression[i],"(%s*%s)",expa , expb); number[i] = a * b; if ( Search(n - 1) ) return 1; if (b != 0) { sprintf(expression[i],"(%s/%s)",expa , expb); number[i] = a / b; if ( Search(n - 1) ) return 1; } if (a != 0) { sprintf(expression[i],"(%s/%s)",expb , expa); number[i] = b / a; if ( Search(n - 1) ) return 1; } number[i] = a; number[j] = b; strcpy(expression[i] , expa); strcpy(expression[j] , expb); } } return 0;}void main(){ int i,x; char buffer[20]; printf("请输入4个数:"); for (i = 0; i < COUNT_OF_NUMBER; i++) { scanf("%d",&x); number[i] = x; itoa(x, buffer, 10); strcpy(expression[i] , buffer); } if ( !Search(COUNT_OF_NUMBER) ) printf("无解!\n");}