Cod sursa(job #1131005)

Utilizator flemixFiru Denis flemix Data 28 februarie 2014 17:07:03
Problema Combinari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
using namespace std;
int k,p,st[100],n;
ofstream f("combinari.out");
int succesor()
{
    if(st[k]<n)
    {
        st[k]++;
        return 1;
    }
    else
    {
        return 0;
    }
}
void init(){st[k]=0;}
int valid()
{
    int i;
    for(i=1;i<=k-1;i++)
    {
        if(st[i]==st[k])
        {
            return 0;
        }
    }
    if(k>1 && st[k-1]>st[k])
    {
        return 0;
    }
    return 1;

}
int sol()
{
    return k==p;
}
void tipar()
{
    int i;
    for(i=1;i<=k;i++)
    {
        f<<st[i]<<" ";
    }
    f<<"\n";
}
void backt()
{
    int as;
    k=1;
    init();
    while(k>0)
    {
        while((as=succesor())&&(!valid()))
        {

        }
        if(!as)
        {
            k--;
        }
        else
        {
            if(sol())
            {
                tipar();
            }
            else
            {
                k++;
                init();
            }
        }
    }
}
int main()
{
    ifstream f("combinari.in");
    f>>n>>p;
    backt();
    return 0;
}