| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:输入M=6要求输出6=6,6=5+1,6=4+2,6=4+1+1.。。。。6=1+1+1+1+1+1。。
取消只看楼主 加入收藏
汐杨
Rank: 2
等 级:论坛游民
帖 子:27
专家分:13
注 册:2010-7-26
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:0 
输入M=6要求输出6=6,6=5+1,6=4+2,6=4+1+1.。。。。6=1+1+1+1+1+1。。
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int M;
    int *a, c = 0;
    int n, s, i;

    printf("Input Number:");
    scanf("%d", &M);
    a = malloc(sizeof(int) * M);   /*这句什么意思,怎么理解?*/
    n = s = M;
    while(c < M)
    {
        for(i = s; n; i--)
            while(n >= i)
            {
                a[c++] = i;
                n -= i;
            }
        printf("%d=%d", M, a[0]);
        for(i = 1; i < c; i++)
            printf("+%d", a[i]);
        printf("\n");
        if(c < M)
        {
            for(i = c - 1; a[i] == 1; i--)
                n++;
            n += a[i];
            s = a[i] - 1;
            c = i;
        }
    }
    return 0;
}
求一个用递归做的代码!
搜索更多相关主题的帖子: include 
2011-05-18 16:27
快速回复:输入M=6要求输出6=6,6=5+1,6=4+2,6=4+1+1.。。。。6=1+1+1+1+1+1。 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016494 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved