next_permutation函数与perv_permutation函数
next_permutation函数与perv_permutation函数声明:#include <algorithm>
注意:两个函数初始分别为1,2,3 3,2,1否有不同的输出
next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。
如(1 2 3排列)1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{int a[100];a[0]=1;a[1]=2;a[2]=3;do{cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;//注意如果开始是这样a[1],a[0],a[2],顺序又不同了}while(next_permutation(a,a+3));//参数3代表排列长度,注意如果改为2就不同了return 0;
}
prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。(与上相反)
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{int a[100];a[0]=3;a[1]=2;a[2]=1;do{cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;//注意如果开始是这样a[1],a[0],a[2],顺序又不同了}while(prev_permutation(a,a+3));//参数3代表排列长度,注意如果改为2就不同了return 0;
}
/*
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.*/
如下例题:
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 ≤ N ≤ 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:
3 1 2 4
4 3 6
7 9
16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Multiple test cases. For each case:
* Line 1: Two space-separated integers: N and the final sum.
For each case, output one line : An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
4 16
3 1 2 4
Explanation of the sample:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{int n,sum,i,j;while(~scanf("%d%d",&n,&sum)){int a[12][12]={0};int b[10]={1,2,3,4,5,6,7,8,9,10};do{for(i=0;i<n;i++)a[0][i]=b[i];for(i=1;i<n;i++)for(j=0;j<n-i;j++)a[i][j]=a[i-1][j]+a[i-1][j+1];if(a[n-1][0]==sum)break; }while(next_permutation(b,b+n));for(i=0;i<n;i++){if(i!=0)printf(" ");//cout<<" ";printf("%d",b[i]);//cout<<b[i];}printf("\n");}return 0;
}
next_permutation函数与perv_permutation函数相关推荐
- render函数和redirect函数的区别+反向解析
render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象) request对象:----->所有的请求 ...
- Python day10 global关键字、函数递归、匿名函数、map函数的用法详解
1.global关键字 引用全局变量,在局部全局变量改变,也会改变,global相当于指针,将地址指向全局变量的name name='littlepage'def littepage():global ...
- C++ 笔记(13)— 函数(函数声明、函数定义、函数调用[传值、指针、引用]、函数参数默认值、函数重载)
每个 C++ 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数. 1. 函数声明 函数声明告诉编译器函数的名称.返回类型和参数.函数声明包括以下几个部分: ret ...
- Go 学习笔记(16)— 函数(02)[函数签名、有名函数、匿名函数、调用匿名函数、匿名函数赋值给变量、匿名函数做回调函数]
1. 函数签名 函数类型也叫做函数签名,可以使用 fmt.Printf("%T") 格式化参数打印函数类型. package mainimport "fmt"f ...
- Go 学习笔记(15)— 函数(01)[函数定义、函数特点、多值返回、实参形参、变长参数,函数作为参数调用]
1. 函数定义 Go 语言最少有个 main() 函数.函数声明告诉了编译器函数的名称,返回类型和参数. func funcName(parameter_list)(result_list) {fun ...
- MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据
1. 字符串函数 MySQL 的常用函数包括字符串函数.数值函数.日期时间函数.流程函数等. SELECT ascii("abc"),char(97),concat("h ...
- 经常可能会用到的【函数节流和函数防抖】记录下,做下区分
今天突然被人问到,函数节流和函数防抖的区别是什么, 结果我脑子一热直接举了个滚动条的粟子说是优化高频率执行的手段,就记得自己是用setTimeout来实现的. 完了区别是什么??哪个是哪个都蒙B了 回 ...
- c语言随机数生成0 99函数,C语言生成随机数的函数、延时函数
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 下面C语言代码使用了生成随机数的函数.延时函数.请大家仔细观察其显示效果. 从以下代码,我们可以得出一个重要的结论:当上述两类函数被放入循环时,应作出一定 ...
- 在python中使用关键字define定义函数_python自定义函数def的应用详解
这里是三岁,来和大家唠唠自定义函数,这一个神奇的东西,带大家白话玩转自定义函数 自定义函数,编程里面的精髓! def 自定义函数的必要函数:def 使用方法:def 函数名(参数1,参数2,参数-): ...
最新文章
- SpringBoot 【IDEA热部署+浏览器禁用缓存】迅速提升效率
- QIIME 2教程. 29参考数据库DataResources(2021.2)
- anaconda安装yolov3_YOLOv3_图像识别_神经网络_人工智能
- 【渣硕的数学笔记】数值分析
- ubuntu运行Faster R-CNN
- » Markdown/reST 编辑器 ReText 3.0 发布 Wow! Ubuntu
- 位运算判断奇偶数_位运算符判断奇偶
- C语言学习及项目开发所遇问题总集(一)---Mr.Zhang
- TODO算子-双Value类型的操作
- 11. SpringMVC拦截器(资源和权限管理)
- 2021-08-04 模糊查询
- ubuntu离线安装fish
- Linux下用openmp速度反而慢,c-为什么ubuntu 12.04下的OpenMP比串行版本慢
- react脚手架配置代理
- SIM868中的GPRS调试失败的几个原因
- 【Format】ASF/WMV 文件格式解析
- 光猫DNS服务器未响应,有光纤猫了还要猫吗?
- Jenkins集成Django
- iphone计算机怎样打开声音,8个iOS必知小技巧!iPhone的声音还可以调更大哦!
- 录屏工具ScreenToGif功能总结
热门文章
- windows下的OpenGL视频播放器开发环境
- 谁能给个oracle邮箱,谁能给我一个电子邮箱
- boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.Output
- 【安全牛学习笔记】密码嗅探
- Java项目:SSM设备台账管理系统
- 计算机辅助制造题库选择题,《机械设计基础习题库
- 微信的小程序和小游戏的区别
- win10运行bat脚本,提示Permission denied
- 记一次阿里云ECS实例预约迁移
- 解决springcloud集成nacos 使用lb 无效