山大工大联谊1006 combinatorial mathematics

1006 combinatorial mathematics

description

As you know, shadow95 is pretty good at maths, especially combinatorial mathematics. Now, he has made a problem for you. We call a subset which exactly has r elements as a "r-subset".For example, {1,2,5} is a 3-subset(r=3) of {1,2,3,4,5}. Now, your task is to form all the r-subset of {1,2,...,n}, then output them in lexicographic order(字典序).

input
The input file ends by EOF.
For each test case, there are two integer n,r.(1<=r<n<=20,r<n/2)

output
First output the case number, then output all the r-subset of {1,2,...,n} in lexicographic order.
Each case seperates by a blank line.

sample input
3 2
3 1
sample output
Case #1:
1 2
1 3
2 3


Case #2:
1
2
3



题意:求n的所有r长子串。
分析:签到题


#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
#include <map>
#include <queue>
#include <set>
using namespace std;
//2015.3.29
int t;
int n,r;
int a[30];
int main()
{
    t=1;
    while(true)
    {
       cin>>n>>r;
       printf("Case #%d:\n",t);
       for(int i=1;i<=r;i++)
       {
        a[i]=i;
        printf("%d ",a[i]);
       }
       cout<<endl;
       for(int i=r;i>=1;i--)
       {
        while(a[i]<n)
        {
            a[i]++;
            if(i!=r)
            {
                int j;
                for(j=i+1;j<=r;j++)
                {
                    a[j]=a[j-1]+1;
                }
            }
            if(a[r]>n)break;
            for(int k=1;k<=r;k++)
            {
                printf("%d ",a[k]);
            }
            cout<<endl;
            i=r;
        }
       }
       t++;
    }
    return 0;
}


你可能感兴趣的:(String,ACM)