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.*/

如下例题:

USACO 2006 February Silver—Backward Digit Sums
Description

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.

Input

Multiple test cases. For each case:

* Line 1: Two space-separated integers: N and the final sum.

Output

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.

Sample Input

4 16

Sample Output

3 1 2 4

Hint

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函数相关推荐

  1. render函数和redirect函数的区别+反向解析

    render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象)   request对象:----->所有的请求 ...

  2. Python day10 global关键字、函数递归、匿名函数、map函数的用法详解

    1.global关键字 引用全局变量,在局部全局变量改变,也会改变,global相当于指针,将地址指向全局变量的name name='littlepage'def littepage():global ...

  3. C++ 笔记(13)— 函数(函数声明、函数定义、函数调用[传值、指针、引用]、函数参数默认值、函数重载)

    每个 C++ 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数. 1. 函数声明 函数声明告诉编译器函数的名称.返回类型和参数.函数声明包括以下几个部分: ret ...

  4. Go 学习笔记(16)— 函数(02)[函数签名、有名函数、匿名函数、调用匿名函数、匿名函数赋值给变量、匿名函数做回调函数]

    1. 函数签名 函数类型也叫做函数签名,可以使用 fmt.Printf("%T") 格式化参数打印函数类型. package mainimport "fmt"f ...

  5. Go 学习笔记(15)— 函数(01)[函数定义、函数特点、多值返回、实参形参、变长参数,函数作为参数调用]

    1. 函数定义 Go 语言最少有个 main() 函数.函数声明告诉了编译器函数的名称,返回类型和参数. func funcName(parameter_list)(result_list) {fun ...

  6. MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据

    1. 字符串函数 MySQL 的常用函数包括字符串函数.数值函数.日期时间函数.流程函数等. SELECT ascii("abc"),char(97),concat("h ...

  7. 经常可能会用到的【函数节流和函数防抖】记录下,做下区分

    今天突然被人问到,函数节流和函数防抖的区别是什么, 结果我脑子一热直接举了个滚动条的粟子说是优化高频率执行的手段,就记得自己是用setTimeout来实现的. 完了区别是什么??哪个是哪个都蒙B了 回 ...

  8. c语言随机数生成0 99函数,C语言生成随机数的函数、延时函数

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 下面C语言代码使用了生成随机数的函数.延时函数.请大家仔细观察其显示效果. 从以下代码,我们可以得出一个重要的结论:当上述两类函数被放入循环时,应作出一定 ...

  9. 在python中使用关键字define定义函数_python自定义函数def的应用详解

    这里是三岁,来和大家唠唠自定义函数,这一个神奇的东西,带大家白话玩转自定义函数 自定义函数,编程里面的精髓! def 自定义函数的必要函数:def 使用方法:def 函数名(参数1,参数2,参数-): ...

最新文章

  1. SpringBoot 【IDEA热部署+浏览器禁用缓存】迅速提升效率
  2. QIIME 2教程. 29参考数据库DataResources(2021.2)
  3. anaconda安装yolov3_YOLOv3_图像识别_神经网络_人工智能
  4. 【渣硕的数学笔记】数值分析
  5. ubuntu运行Faster R-CNN
  6. » Markdown/reST 编辑器 ReText 3.0 发布 Wow! Ubuntu
  7. 位运算判断奇偶数_位运算符判断奇偶
  8. C语言学习及项目开发所遇问题总集(一)---Mr.Zhang
  9. TODO算子-双Value类型的操作
  10. 11. SpringMVC拦截器(资源和权限管理)
  11. 2021-08-04 模糊查询
  12. ubuntu离线安装fish
  13. Linux下用openmp速度反而慢,c-为什么ubuntu 12.04下的OpenMP比串行版本慢
  14. react脚手架配置代理
  15. SIM868中的GPRS调试失败的几个原因
  16. 【Format】ASF/WMV 文件格式解析
  17. 光猫DNS服务器未响应,有光纤猫了还要猫吗?
  18. Jenkins集成Django
  19. iphone计算机怎样打开声音,8个iOS必知小技巧!iPhone的声音还可以调更大哦!
  20. 录屏工具ScreenToGif功能总结

热门文章

  1. windows下的OpenGL视频播放器开发环境
  2. 谁能给个oracle邮箱,谁能给我一个电子邮箱
  3. boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.Output
  4. 【安全牛学习笔记】密码嗅探
  5. Java项目:SSM设备台账管理系统
  6. 计算机辅助制造题库选择题,《机械设计基础习题库
  7. 微信的小程序和小游戏的区别
  8. win10运行bat脚本,提示Permission denied
  9. 记一次阿里云ECS实例预约迁移
  10. 解决springcloud集成nacos 使用lb 无效