回复 2楼 rjsp
那大佬,怎么将这个C++程序改为C语言程序?(我主要把cout改为printf后,里面的内容不会改)
# include <iostream>
using namespace std;
double sum(int a, int b)//加法
{
return (a + b);
}
double sub(int a, int b)//减法
{
return (a - b);
}
double mul(int a, int b)//乘法
{
return (a * b);
}
double divi(int a, int b)//除法
{
return (a / b);
}
int main()//主函数
{
int a, b, c, d;
int kinds = 0;//kinds表示算法的总数
cout << "请输入4个数字\n";
cin >> a >> b >> c >> d;
double (*suan24[4])(int, int) = { sum,sub,mul,divi };//主要的算法函数
char p[4] = { '+','-','*',' /' };
//第一种顺序
for (int i1 = 0; i1 < 4; i1++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i1](suan24[j](suan24[m](a, b), c), d);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << a << p[m] << b << ")" << p[j] << c << ")" << p[i1] << d << "=24" << endl;
kinds += 1;
}
}
}
}
//第二种顺序
for (int i2 = 0; i2 < 4; i2++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i2](suan24[j](suan24[m](b, c), a), d);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << b << p[m] << c << ")" << p[j] << a << ")" << p[i2] << d << "=24" << endl;
kinds += 1;
}
}
}
}
//第三种顺序
for (int i3 = 0; i3 < 4; i3++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i3](suan24[j](suan24[m](c, d), b), a);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << c << p[m] << d << ")" << p[j] << b << ")" << p[i3] << a << "=24" << endl;
kinds += 1;
}
}
}
}
//第四种顺序
for (int i4 = 0; i4 < 4; i4++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i4](suan24[j](suan24[m](b, c), d), a);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << b << p[m] << c << ")" << p[j] << d << ")" << p[i4] << a << "=24" << endl;
kinds += 1;
}
}
}
}
//第五种顺序
for (int i5 = 0; i5 < 4; i5++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i5](suan24[j](suan24[m](a, c), b), d);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << a << p[m] << c << ")" << p[j] << b << ")" << p[i5] << d << "=24" << endl;
kinds += 1;
}
}
}
}
//第六种顺序
for (int i6 = 0; i6 < 4; i6++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i6](suan24[j](suan24[m](a, c), d), b);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << a << p[m] << c << ")" << p[j] << d << ")" << p[i6] << b << "=24" << endl;
kinds += 1;
}
}
}
}
//第七种顺序
for (int i7 = 0; i7 < 4; i7++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i7](suan24[j](suan24[m](a, d), b), c);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << a << p[m] << d << ")" << p[j] << b << ")" << p[i7] << c << "=24" << endl;
kinds += 1;
}
}
}
}
//第八种顺序
for (int i8 = 0; i8 < 4; i8++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i8](suan24[j](suan24[m](a, d), c), b);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << a << p[m] << d << ")" << p[j] << c << ")" << p[i8] << b << "=24" << endl;
kinds += 1;
}
}
}
}
//第九种顺序
for (int i9 = 0; i9 < 4; i9++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i9](suan24[j](suan24[m](b, d), c), a);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << b << p[m] << d << ")" << p[j] << c << ")" << p[i9] << a << "=24" << endl;
kinds += 1;
}
}
}
}
//第十种顺序
for (int i10 = 0; i10 < 4; i10++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i10](suan24[j](suan24[m](b, d), a), c);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << b << p[m] << d << ")" << p[j] << a << ")" << p[i10] << c << "=24" << endl;
kinds += 1;
}
}
}
}
//第十一种顺序
for (int i11 = 0; i11< 4; i11++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i11](suan24[j](suan24[m](c, d), a), b);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << c << p[m] << d << ")" << p[j] << a << ")" << p[i11] << b << "=24" << endl;
kinds += 1;
}
}
}
}
//第十二种顺序
for (int i12 = 0; i12 < 4; i12++)
{
for (int j = 0; j < 4; j++)
{
for (int m = 0; m < 4; m++)
{
int result = suan24[i12](suan24[j](suan24[m](b, a), c), d);
if (result == 24)
{
cout << "这四个数能算出24,其中一种解法为:\n";
cout << "(" << "(" << b << p[m] << a << ")" << p[j] << c << ")" << p[i12] << d << "=24" << endl;
kinds += 1;
}
}
}
}
if (kinds == 0)
{
cout << "这一组数不能算出24点\n";
}
else
cout << "这组数一共有" << kinds << "种算法\n";
return 0;
}