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;
    }

  • 相关阅读:
    平台调用中的数据封送处理
    JavaScript 中的事件流
    Jquery插件 表格固定表头
    ASP.NET MVC Action Filter与内置的Filter实现
    getCurrentScript的改进
    analyze spring framework source
    Windows Azure: Service Bus Brokered Messaging DeadLetterQueue 使用详解
    C#截图
    权限系统
    音乐播放器
  • 原文地址:https://www.cnblogs.com/lisijie/p/7206007.html
Copyright © 2011-2022 走看看