zoukankan      html  css  js  c++  java
  • Ignatius and the Princess II

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1027

    题目是要:求n个数的第m个全排列

    代码:(超时)

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=1005;
    int main()
    {
        int n,a[N],m;
        while (cin>>n>>m)
        {
            int num=0;
            for (int i=0;i<n;i++)
                a[i]=i+1;
            do
            {
                num++;
                if (num==m)
                {
                    for (int i=0;i<n;i++)
                    {
                        if (i!=n-1)
                        cout << a[i] << ' ';
                        else
                        cout << a[i] << endl;
                    }
                }
            }
           while(next_permutation(a,a+n)) ;

        }
        return 0;
    }

    上面这个代码,提交的答案是超时,因为找到第m个全排列以后忘记break了;

    代码:(AC)

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=1005;
    int main()
    {
        int n,a[N],m;
        while (cin>>n>>m)
        {
            int num=0;
            for (int i=0;i<n;i++)
                a[i]=i+1;
            do
            {
                num++;
                if (num==m)
                {
                    for (int i=0;i<n;i++)
                    {
                        if (i!=n-1)
                        cout << a[i] << ' ';
                        else
                        cout << a[i] << endl;
                    }
                    break;
                }
            }
           while(next_permutation(a,a+n)) ;

        }
        return 0;
    }

  • 相关阅读:
    对于delphi 三层的理解
    XE6调用android标准功能
    修复 XE7 Frame 内 PopupMenu 快捷键失效问题 by 龟山阿卍
    最大熵模型 二
    最大熵模型
    算法复习-平面内极大值点
    算法复习-生成全排列
    算法复习-归并排序
    算法复习-快速排序
    连续特征的离散化
  • 原文地址:https://www.cnblogs.com/lisijie/p/7206007.html
Copyright © 2011-2022 走看看