Ignatius and the Princess II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6191    Accepted Submission(s): 3664

Problem Description
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if
you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."

"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once
in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?

 
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of
file.
 
Output
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
 
Sample Input
6 4
11 8
 
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
 
最近学习了下C++的STL模版,知道了迭代器和vector的初步使用。由于还没学到关于序列的知识,只能去搜题解,发现了一个在头文件<algorithm>中神奇的函数next_permutation
 
 

 
 
可以看出他的参数为某种迭代器,然后就用STL的vector来取代数组(其实这题用数组也可以,数组的地址就是一种指针,而迭代器是一种广义指针)
用vector的代码如下:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(void)
{
int n,m;
while(cin>>n>>m)
{
vector<int>list(n);
vector<int>::iterator it;
int k=1;
for (it=list.begin(); it!=list.end(); it++)
{
*it=k;
k++;
}
int COUNT=1;注意此题的 first sequence已经是题中所给的自然增长序列,无需计入
while (next_permutation(list.begin(),list.end()))
{
COUNT++;
if(COUNT==m)
break;
}
for (it=list.begin(); it!=list.end(); it++)
{
if(it!=list.end()-1)
cout<<*it<<' ';
else
cout<<*it<<endl;
}
}
return 0;
}
用数组的代码如下:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(void)
{
int n,m;
while(cin>>n>>m)
{
int *list=new int[n];
int i;
for (int i=0; i<n; i++)
{
list[i]=i+1;
}
int COUNT=1;注意此题的 first sequence已经是题中所给的自然增长序列,无需计入
while (next_permutation(list,list+n))
{
COUNT++;
if(COUNT==m)
break;
}
for (i=0; i<n; i++)
{
if(i!=n-1)
cout<<list[i]<<' ';
else
cout<<list[i]<<endl;
}
delete []list;
}
return 0;
}

HDU——1027Ignatius and the Princess II(next_permutation函数)的更多相关文章

  1. hdu Ignatius and the Princess II

    Ignatius and the Princess II Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Ja ...

  2. 杭电1027Ignatius and the Princess II模拟

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1027 题目: Problem Description Now our hero finds the doo ...

  3. HDU Ignatius and the Princess II 全排列下第K大数

    #include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include& ...

  4. HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  5. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  6. HDU 1027 Ignatius and the Princess II(求第m个全排列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/10 ...

  7. HDU - 1027 Ignatius and the Princess II 全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  8. (全排列)Ignatius and the Princess II -- HDU -- 1027

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/100 ...

  9. HDU 1027 Ignatius and the Princess II(康托逆展开)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

随机推荐

  1. Android(java)学习笔记131:关于构造代码块,构造函数的一道面试题(华为面试题)

    1. 代码实例: package text; public class TestStaticCon { public static int a = 0; static { a = 10; System ...

  2. Problem D: 小平查密码

    Problem D: 小平查密码 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 194  Solved: 40[Submit][Status][Web ...

  3. 2406: C语言习题 求n阶勒让德多项式

    2406: C语言习题 求n阶勒让德多项式 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 961  Solved: 570[Submit][Status ...

  4. java基础—this关键字

    一.this关键字

  5. 如何通过修改文件添加用户到sudoers上

    su - root  chmod u+w /etc/sudoers   (该文件没有写权限, 修改)vim /etc/sudoers 按下 I 键进行编写 # User privilege speci ...

  6. Bootstrap标签页(Tab)插件事件

    事件 下表列出了标签页(Tab)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 show.bs.tab 该事件在标签页显示时触发,但是必须在新标签页被显示之前.分别使用 even ...

  7. Bootstrap历练实例:动画的进度条

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. cocos2d-x之CCCardinalSplineBy

    CCCardinalSplineBy概念 这个类是样条曲线动作,其创建函数CCCardinalSplineBy::create(float duration, cocos2d::CCPointArra ...

  9. java上传附件,批量下载附件(一)

    上传附件代码:借助commons-fileupload-1.2.jar package com.str; import java.io.BufferedInputStream;import java. ...

  10. Python中类的声明,使用,属性,实例属性,计算属性及继承,重写

    Python中的类的定义以及使用: 类的定义: 定义类 在Python中,类的定义使用class关键字来实现 语法如下: class className: "类的注释" 类的实体 ...