| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1611 人关注过本帖
标题:有大佬会把下面的C++程序改为C语言程序吗?
只看楼主 加入收藏
reburn
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-12-31
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
有大佬会把下面的C++程序改为C语言程序吗?
# 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;
}
搜索更多相关主题的帖子: cout int for 个数 result 
2020-12-31 21:03
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1509
专家分:9241
注 册:2010-3-16
收藏
得分:20 
头文件换成#include <stdio.h>,using那句不要
cout一律换成printf
cin一律换成scanf
2020-12-31 21:39
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1509
专家分:9241
注 册:2010-3-16
收藏
得分:0 
你把a,b,c,d改成数组,然后在穷举呢。
循环里面尽量不要声明变量,声明变量也要耗费cpu时间,i1-i12统一用一个就可以。
你这里算出来的不全。
2020-12-31 22:35
reburn
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-12-31
收藏
得分:0 
回复 2楼 apull
大佬好,那cin改为printf后,里面的内容怎么改?例如:
cout << "(" << "(" << a << p[m] << b << ")" << p[j] << c << ")" << p[i1] << d << "=24" << endl;
2021-01-01 11:24
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1509
专家分:9241
注 册:2010-3-16
收藏
得分:0 
printf("((%d%c%d)%c%d)%c%d=24\n", a, p[m], b, p[j], c, p[i], d);
2021-01-01 11:58
快速回复:有大佬会把下面的C++程序改为C语言程序吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044274 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved