【SSE-HARD】

##Question
满足特异条件的数列。输入m和n(20≥m≥n≥0),求出满足以下方程式的正整数数列i1,i2,…,in,使得i1+i2+…+in=m,且i1≥i2≥…≥in。例如:
当n=4,m=8时,将得到如下5个数列:
5 1 1 1 4 2 1 1 3 3 1 1 3 2 2 1 2 2 2 2
**输入格式要求:“%d” 提示信息:“Please enter requried terms (<=10):”
" their sum:"
**输出格式要求:“There are following possible series:\n” “[%d]:” “%d”
程序运行示例1:
Please enter requried terms (<=10): 4 8
their sum:There are following possible series:
[1]:5111
[2]:4211
[3]:3311
[4]:3221
[5]:2222
程序运行示例2:
Please enter requried terms (<=10):4 10
their sum:There are following possible series:

##Code

#include <stdio.h>

int num[11];

int count = 0;
void dfs(int n, int now, int m, int max){
    if(now==n){
        if(max>=m&&m>=1){
            num[n] = m;
            count++;
            printf("[%d]:",count);
            for(int i=1; i<=n; i++){
                printf("%d",num[i]);
            }
            printf("\n");
        }
        return;
    }
    for(int i=max; i>=m/(n-now+1); i--){
        num[now] = i;
        dfs(n, now+1, m-i, i);
    }
}

int main(){
    printf("Please enter requried terms (<=10):");
    int n,m;
    scanf("%d %d",&n, &m);
    printf("                             their sum:");
    printf("There are following possible series:\n");
    dfs(n,1,m,m-n+1);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值