zoukankan      html  css  js  c++  java
  • SDUT3143:Combinatorial mathematics(组合数学)

    题意:传送门

    题目描述

    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(字典序).

    输入

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

    输出

     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.

    示例输入

    3 2
    3 1

    示例输出

    Case #1:
    1 2
    1 3
    2 3
    
    Case #2:
    1
    2
    3
    题目很简单,代码很快就敲出来了,令人伤心的是PE无数遍,因为之前的题都没卡那么严,遇到这种输出格式错误慌了.......
    每组数据之后有一个空行,但是最后一组数据没有空行,如果只输入一组n,m,那么之后不需要打印空行。
    具体请看代码:
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <map>
    #include <queue>
    #include <stack>
    #define inf 0x3f3f3f3f
    #include <stdio.h>
    #include <string.h>
    typedef long long ll;
    #define mod 10000007
    #define eps 1e-9
    using namespace std;
    int n,r,a[30];
    void dfs(int k,int step)
    {
        a[step]=k;
    if(step>r) return ;
    if(step==r) { for(int i=1; i<=step; i++) { if(i==1) printf("%d",a[i]); else printf(" %d",a[i]); } printf(" "); } for(int i=k+1; i<=n; i++) dfs(i,step+1); } int main() { int K=0; while(scanf("%d%d",&n,&r)!=EOF) { if(K>=1) printf(" ");//这样能避免PE printf("Case #%d: ",++K); for(int i=1; i<=n-r+1; i++) dfs(i,1); } return 0; }
  • 相关阅读:
    安全购买数码相机的诀窍!(1)
    获得网卡MAC地址和IP地址
    用Asp.net实现基于XML的留言簿之二
    安全购买数码相机的诀窍!(2)
    使用Flash读取COOKIE
    数码常识:CCD的原理
    ACE 5.5 Make in RedHat AS 4 Update 4 Issue
    Eclipse Plugins 开发 (1)
    RedHat AS4 Update4 DNS (bind 9) 配置
    Maven2 & Continuum 持续整合 (1)
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/4328358.html
Copyright © 2011-2022 走看看