1004 “字典序” 解题报告

To make the problem easier, we can only generate a line of the matrix.

There are 362880 different kinds of line from 123456789 to 987654321.

In this problem,we regard 123456789 as the first line,regard 123456798

as the second line, and so on.

输入

The input consists of multiple test cases. Each test case consists of an integer n(0<n<=362880). Process to the end of file.

输出

For each test case, print the nth line.

样例输入

1
2
3
362880

样例输出

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
9 8 7 6 5 4 3 2 1

分析:题目BB了一段之后意思就是有一个数组a[9]={1,2,3,4,5,6,7,8,9};输入一个n,然后输出第n个字典序列。。。第一遍做的时候我也没看懂,听说有个函数可以用,我就直接贴代码ac了这道题。。。没想到今天作业赛又出现了这道题。。

代码:

#include <stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9},n,i;
    while (scanf("%d",&n)!=EOF)
    {
        do
        {
            n--;
            if (n==0)
            {
                for(i=0;i<9;i++)
                {
                    printf("%d",a[i]);
                    if(i!=8)
                        putchar(' ');
                    if(i==8)
                        putchar(10);
                }
            }
        }while (next_permutation(a,a+9));
    }
 return 0;

}
这就是运行结果。。后来我百度了一下next_permutation函数,大概用法如下:(这一段是摘抄)

int类型的permutation函数:

    do
        {
            n--;
            if (n==0)
            {
                for(i=0;i<n-1;i++)//输出
                {
                    printf("%d",a[i]);
                    if(i!=8)
                        putchar(' ');
                    if(i==n-1)
                        putchar(10);
                }
            }
       <font color="#FF0000"> }while (next_permutation(a,a+n));<span style="font-size:18px;"><strong>//参数n指的是要进行排列的长度
 <wbr>
//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
</wbr></strong><span style="color:#000000;">下面讲<span style="font-size:18px;">prev_permutation:</span></span><strong>
</strong></span></font>
#include <stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9},n,i;
    while (scanf("%d",&n)!=EOF)
    {
        do
        {
            n--;
            if (n==0)
            {
                for(i=0;i<9;i++)
                {
                    printf("%d",a[i]);
                    if(i!=8)
                        putchar(' ');
                    if(i==8)
                        putchar(10);
                }
            }
        }while (prev_permutation(a,a+9));
    }
 return 0;

}

结果就是:

当然这个函数还有很多用法。。(一下是摘抄)

(2) char 类型的next_permutation
 
int main()
{
 char ch[205];
cin >> ch;
 
sort(ch, ch + strlen(ch) );
//该语句对输入的数组进行字典升序排序。如输入9874563102cout<<ch;将输出0123456789,这样就能输出全排列了
 
 char *first = ch;
 char *last = ch + strlen(ch);
 
 do {
cout<< ch<< endl;
}while(next_permutation(first, last));
 return 0;
}
 
//这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
//若采用 while(next_permutation(ch,ch+5));如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符

 
 
 
 
 
(3) string 类型的next_permutation
 
int main()
{
 string line;
 while(cin>>line&&line!="#")
{
 if(next_permutation(line.begin(),line.end()))//从当前输入位置开始
cout<<line<<endl;
 elsecout<<"Nosuccesor\n";
}
}
 
 
 
int main()
{
 string line;
 while(cin>>line&&line!="#")
{
sort(line.begin(),line.end());//全排列
cout<<line<<endl;
 while(next_permutation(line.begin(),line.end()))
cout<<line<<endl;
}
}
 
 
 
 
 
 
 next_permutation 自定义比较函数
 
 
#include<iostream> //poj 1256Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b)//'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
 if(tolower(a)!=tolower(b))
 returntolower(a)<tolower(b);
 else
 return a<b;
}
int main()
{
 char ch[20];
 int n;
cin>>n;
 while(n--)
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
 do
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen(ch),cmp));
}
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值