欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

算法模板之全排列(next_permutation实现)(HDU 1027 与 HDU 1755)

程序员文章站 2022-03-22 16:25:10
...

实战模板题 :HDU 1027 Ignatius and the Princess II

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
	int a[1010];
	for(int i = 0; i < 1010; i++)
		a[i] = i;
	int m, n;
	while(cin >> n >> m)
	{
		for(int i = 0; i < 1010; i++)
			a[i] = i;
		for(int i = 1; i < m; i++)
			next_permutation(a + 1, a + 1 + n);
		for(int i = 1; i < n; i++)
			cout << a[i] << " ";
		cout << a[n] << endl; 
	}
	return 0;

实战模板题:HDU 1755 A Number Puzzle
#include<cstdio>  
#include<iostream> 
#include<algorithm>  

using namespace std; 
 
int m, n, k, t, x;
int num[15];
int number[1000000];

int main()  
{  
    while(~scanf("%d%d", &n , &m))
	{
    	for(int i = 0; i < n; i++)
    		scanf("%d", &num[i]);
		sort(num, num + n);
    	int p = 0;
		do{
			if(num[0] == 0) continue;
			int ans = 0;
    		for(int j = 0; j < n; j++)
    		{
    			ans = ans * 10 + num[j];
			}
			number[p++] = ans;
		}while(next_permutation(num, num + n));
	
		for(int i = 0; i < m; i++)
    	{
    		scanf("%d%d", &x , &k);
			int flag = 1;
			for(int j = 0; j < p; j++)
			{
				if((number[j] + x) % k == 0)
				{
					cout << number[j] << endl;
					flag = 0;
					break;
				}
			}
		if(flag) cout << "None" << endl;
		}
	}
    return 0;  
}