STL中next_permutation函数快速上手
来源:互联网 发布:淘宝手机下单减价 编辑:程序博客网 时间:2024/06/10 19:59
用法实验
- next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。
- 例如当前是213,下一个排列就是231.这里是输出0123的全排列实验。
#include<algorithm>#include<iostream>using namespace std;int main(){ int a[4]={0,1,2}; do{ cout<<a[0]<<a[1]<<a[2]<<endl; }while(next_permutation(a,a+3)); cout<<a[0]<<a[1]<<a[2]<<endl; return 0;}- 最后一个排列没有下一个排列,用next_permutation会返回false,但是使用了这个方法后,序列会变成字典序列的第一个,如210变成012。- 当然还有一个prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。
具体案例
题目描述
这是一道简单的小学生算术题,问题是这样的:给你四个整型数A,B,C,D,问其中的某一个数字能否通过其他三个数的加减乘除运算得到。每个数只能用一次。如果可以,则输出Yes,否则输出No。比如四个数为1,2,3,6.而1 + 2 + 3 = 6。所以输出Yes。如果四个数为2,2,3,9,则任意的三个数的运算,都不可能让等式成立。所以输出No。提示:在计算机的整型数相除中,5 / 2 = 2,而我们问题中的算术,5 / 2 = 2.5。能帮你的只有这么多了
输入
第一行输入一个整数t,表示有t组测试数据。每组数据,输入四个整数,A,B,C,D。其中0 <= A,B,C,D <= 1000.
输出
对于每组测试数据,按题意输出Yes或者No。
样例输入
2
1 2 3 6
2 2 3 9
样例输出
Yes
No
题解
#include<cstdio>#include<algorithm>#include<iostream>using namespace std;double a[5];bool ok;void dfs(int step,double ans){ if(ok) return; if(step>2){ if(ans==a[3]) ok=true; return; } for(int i=0;i<4;i++){ if(i==0) dfs(step+1,ans+a[step]); else if(i==1) dfs(step+1,ans-a[step]); else if(i==2) dfs(step+1,ans*a[step]); else if(i==3) dfs(step+1,ans/a[step]); }}int main(){ int T; scanf("%d",&T); while(T--){ for(int i=0;i<4;i++) scanf("%lf",&a[i]); ok=false; sort(a,a+4);//先排序 do{ dfs(1,a[0]); if(ok) break; }while(next_permutation(a,a+4)); printf("%s\n",ok?"Yes":"No"); } return 0;}
阅读全文
0 0
- STL中next_permutation函数快速上手
- 详解STL中next_permutation()函数实现
- STL中全排列函数 next_permutation
- STL快速全排列函数next_permutation与pre_permutation
- STL的next_permutation函数
- 【STL】next_permutation函数用法
- C++STL中全排列函数next_permutation的使用
- C++STL中全排列函数next_permutation的使用
- C++STL中全排列函数next_permutation的使用
- STL中的next_permutation函数用法
- STL/next_permutation()和prev_permutation()函数
- 《STL源码剖析》--next_permutation函数
- C++中next_permutation函数
- 排列2 + stl函数+ next_permutation()
- STL之next_permutation() 全排列函数
- hdu1027 全排列(STL函数next_permutation)
- hdu1716 STL next_permutation函数的使用
- STL具体操作之next_permutation和prev_permutation函数
- LRCN(1)
- 打靶
- 练习 2017-8-14 缓冲字节/字符 输入/输出 流~ 字符的编码和解码
- MySQL 的redo 和 undo
- [Linux] yum和apt-get用法及区别
- STL中next_permutation函数快速上手
- github使用教程与问题解决
- 【CodeForces】616A
- 【教程】Win7-64位安装OpenSSL详细过程
- 解决C#编写的程序无法在xp系统上运行的问题
- 浅谈移动端三大viewpor
- String stringBuffer stringbuilder
- ACM常用的解题技巧:尺取法
- Codeforces Round #428 (Div. 2) B 贪心