Cod sursa(job #550354)

Utilizator DevilShadowJunc Raul Cosmin DevilShadow Data 9 martie 2011 13:45:58
Problema Generare de permutari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream.h>

using namespace std;
short int n, i, v[9];
ofstream g ("premutari.out");

void back()
{
	int aux, h, j;
	bool ok = true, okey = false;
	
	for(h = n; h > 0; h --)
	{
		ok = true;
		v[h] ++;
		if(v[h] > n)
		{
			v[h] = 1;
		}
		else
		{
			for(i = 1; i < n; i ++)
				for(h = i + 1; h <= n; h ++)
					if(v[i] == v[h])
						ok = false;
			if(ok)
			{
				for(i = 1; i <= n; i ++)
					g << v[i] << ' ';
				g << "\n";
				break;
			}
		}
	}
	for(j = n; j > 1; j --)
		if(v[j] > v[j - 1])
		{
			okey = true;
			break;
		}
	if(okey)
		back();
}

int main()
{
	ifstream f ("premutari.in");
	
	
	f >> n;
	
	for(i = 1; i <= n; i ++)
	{
		v[i] = i;
		g << v[i] << ' ';
	}
	
	g << '\n';
	
	back();
	
	f.close();
	g.close();
}