鱼C论坛

 找回密码
 立即注册
查看: 1566|回复: 2

[已解决]求助

[复制链接]
发表于 2021-4-11 22:45:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
求大佬们给咱解析解析下面的代码,希望能详细一点,谢谢各位大佬。
#include <algorithm>
#include <cstdio>
using namespace std;

int s[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int main()
{        
    do
    {
            if(!s[0] || !s[4] || !s[8]) continue;
            int a = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
                int b = s[4] * 1000 + s[5] * 100 + s[6] * 10 + s[7];
                int c = s[8] * 10 + s[9];
                if((a - b) * c == 900) printf("(%d-%d)*%d=900\n", a, b, c);

    }while(next_permutation(s, s + 10));

    return 0;
}
最佳答案
2021-4-11 23:55:49
#include <algorithm>
#include <cstdio>
using namespace std;

int s[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int main()
{        
    do
    {
            if(!s[0] || !s[4] || !s[8]) continue;   //当这几个索引位置的值为0时跳过本次循环
            int a = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];     //将数组的前四个整数组成一个四位数a
                int b = s[4] * 1000 + s[5] * 100 + s[6] * 10 + s[7];  //后续四个整数组成另一个四位数b
                int c = s[8] * 10 + s[9];                                            //最后两个整数组成一个两位数c
                if((a - b) * c == 900) printf("(%d-%d)*%d=900\n", a, b, c);    //当(a - b) * c == 900 输出

    }while(next_permutation(s, s + 10));

    return 0;
}


next_permutation(s, s + 10)这里讲一下next_permutation()函数  其实就是全排列函数
给个例子自己看,或者自己查查
vector<char> chars = {'a', 'b', 'c'};
  do {
    cout << chars[0] << chars[1] << chars[2] << endl;
  } while (next_permutation(chars.begin(), chars.end()));
输出为:
abc
acb
bac
bca
cab
cba
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-11 23:55:49 | 显示全部楼层    本楼为最佳答案   
#include <algorithm>
#include <cstdio>
using namespace std;

int s[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int main()
{        
    do
    {
            if(!s[0] || !s[4] || !s[8]) continue;   //当这几个索引位置的值为0时跳过本次循环
            int a = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];     //将数组的前四个整数组成一个四位数a
                int b = s[4] * 1000 + s[5] * 100 + s[6] * 10 + s[7];  //后续四个整数组成另一个四位数b
                int c = s[8] * 10 + s[9];                                            //最后两个整数组成一个两位数c
                if((a - b) * c == 900) printf("(%d-%d)*%d=900\n", a, b, c);    //当(a - b) * c == 900 输出

    }while(next_permutation(s, s + 10));

    return 0;
}


next_permutation(s, s + 10)这里讲一下next_permutation()函数  其实就是全排列函数
给个例子自己看,或者自己查查
vector<char> chars = {'a', 'b', 'c'};
  do {
    cout << chars[0] << chars[1] << chars[2] << endl;
  } while (next_permutation(chars.begin(), chars.end()));
输出为:
abc
acb
bac
bca
cab
cba
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-13 08:07:15 | 显示全部楼层
谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 17:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表