需要C程序员的建议和帮助 [英] Need Advice and help from C programmer

查看:124
本文介绍了需要C程序员的建议和帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将打印以下序列的C程序作为示例,如果n = 5
5
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1

我已经编写了代码,我的一个朋友对其进行了一些更改,现在它可以正常工作了.但是我不明白他做了什么.代码写在下面

I''m writing a C program that will print the following sequence As an Example If n=5
5
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1

i have write a code and one of my friend make some changes to it and now it working properly. But i couldn''t understand what he has done. the code is written here below

#include <stdio.h>

int Min(int Number1, int Number2)
{
	if(Number1 <= Number2)
		return Number1;
	else
		return Number2;
}

void Partition(int Number, int Lim, const char* Str)
{    
    int Count = 0; 
    char Out[50];   
    if (Number > 0)
    {
        for(Count = Min(Number, Lim);Count > 0;Count-- )
        {
            sprintf(Out, "%s %d", Str, Count);
            Partition(Number - Count, Count, Out);
        }
    }
    else
        printf("%s\n", Str);
}

int main()
{
    int Number = 0;
    
    
    char Str[50];
        
    printf("Enter a number: ");
    scanf("%d", &Number);
    
    if (Number < 1)
    {
        printf("Number is Negative.\n");
        return -1;    
    }
    
    sprintf(Str, "%d = ", Number);
    Partition(Number, Number, Str);
    
    return 0;
}



如果您在这里需要任何psedocode帮助,则为:但是我不知道这是否正确.因为我是c plz帮助的初学者.

过程最小值(数字1,数字2)
1.如果Number1< = Number2
2.然后返回Number1
3.否则返回Number2

过程分区(Number,Lim,Str)
1.数0
2.如果Number> 0
3.然后将Min(数字,Lim)的数量减少到1
4.打印(Str,计数)
5.分区(数字-计数,计数,输出)
6. else
7.然后打印(Str)

我们不能使用< math.h>来执行此操作吗?为了跳过该最小功能"



if you need any psedocode help here it is: but i''m not sure wheather this is correct or not. Since i''m beginner for c plz help.

Procedure Min (Number1, Number2)
1.If Number1<=Number2
2. then return Number1
3. else return Number2

Procedure Partition (Number, Lim, Str)
1. count 0
2. if Number > 0
3. then for count Min (Number, Lim) down to 1
4. print (Str, count)
5. Partition(Number-count,count,Out)
6. else
7. then print(Str)

Can''t we do this using <math.h> in order to skip that "Min Function"

推荐答案

好吧,尝试一下,看看会发生什么...
Well, try it and see what happens...


这篇关于需要C程序员的建议和帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆