以下是引用unicorn在2005-12-26 21:47:00的发言:
没时间琢磨 网上有很多吧
----code quoted from google----
#include <stdio.h>
#include<math.h>
float a[4];
void cpl(int,int);
void count24();
float computer(float,float,int);
void main()
{
int i;
printf("Please input 4 number\n");
printf("The number should >0 and <14\n");
for(i=0;i<4;i++){
do{
scanf("%f",&a[i]);
if(a[i]<1||a[i]>13){
printf("The number is error\n");
}
}while(a[i]<1||a[i]>13);
}
cpl(0,4);
}
void cpl(int m, int n)
{
int i;
float t;
if (m<n-1) {
cpl(m+1, n);
for (i=m+1;i<n;i++) {
t=a[m]; a[m]=a[i]; a[i]=t;
cpl(m+1, n);
t=a[m]; a[m]=a[i]; a[i]=t;
}
} else
{
count24();
}
}
void count24()
{
int i,j,k;
float b[3];
char s[5]= "+-*/";
for(i=0;i<4;i++){
for(j=0;j<4;j++){
for(k=0;k<4;k++){
b[0]=computer(a[0],a[1],k);
b[1]=computer(b[0],a[2],j);
b[2]=computer(b[1],a[3],i);
if((abs(b[2]-24))<0.000001){
printf("%f= %f %c %f\n",b[0],a[0],s[k],a[1]);
printf("%f = %f %c %f\n",b[1],b[0],s[j],a[2]);
printf("%f = %f %c %f\n\n\n ",b[2],b[1],s[i],a[3]);
}
}
}
}
}
float computer(float m,float n,int op)
{
float temp;
switch(op){
case 0:
temp=m+n;
break;
case 1:
temp=m-n;
break;
case 2:
temp=m*n;
break;
case 3:
temp=m/n;
break;
}
return temp;
}
引7楼:
for (i=m+1;i<n;i++) {
t=a[m]; a[m]=a[i]; a[i]=t;
cpl(m+1, n);
t=a[m]; a[m]=a[i]; a[i]=t;
}
这一段有什么用??