24小时热门版块排行榜    

查看: 2024  |  回复: 11
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

holmescn

金虫 (正式写手)

[交流] 【分享】关于9个数字求和的C语言程序 已有9人参与

郁闷,刚发上去就被干掉了。我再发一个贴吧。
经过我一下午加一晚上(大约是下午4点到凌晨2点)的连续思考,终于用C实现了这个题目。程序有点长,没有注释,大家凑合围观一下吧。
不明白的地方大家可以相互交流,切磋。呵呵。
CODE:
#include
#include
#include
#include
#include

struct node
{
    int num;
    char op;
    int digits;
};

int Calc(struct node* p, int n);
int myAtoi(char* begin, int length);
int Find(struct node* p, int n, int sum);
bool genNumber(struct node* p, int n, int r);
void toNumbers(struct node* p, int n);
void genOperator(struct node* p, int n, int flag);
void Print(struct node* p, int n);

char numbers[]="123456789";

int myAtoi(char* begin, int length)
{
    int result=0;
    char* r=malloc(length+1);
    r=strncpy(r,begin,length);
    r[length+1]=0;
    result=atoi(r);
    free(r);
    return result;
}
void toNumbers(struct node* p, int n)
{
    char* q=&numbers[0];

    for(int i=0;i     {
        p[i].num=myAtoi(q,p[i].digits);
        q+=p[i].digits;
    }
}

int Calc(struct node* p, int n)
{
    int sum=0;
    sum=p[0].num;
    if(p[0].op=='-')
        sum=0-sum;

    for(int i=1;i     {
        switch(p[i].op)
        {
            case '+':
                sum+=p[i].num;
                break;
            case '-':
                sum-=p[i].num;
                break;
            default:
                break;
        }
    }

    return sum;
}

bool genNumber(struct node* p, int n, int r)
{
    if(r<0)
    {
        p[n+2].digits++;
        p[n+1].digits=0;
        p[n].digits=0;
        return false;
    }

    if(n==0)
    {
        if(r==0)
            return false;
        p[n].digits=r;
        return true;
    }

    if(n==1)
    {
        p[n].digits++;
    }

    if(p[n].digits==0)
    {
        p[n].digits=1;
    }
    genNumber(p, n-1, r-p[n].digits);
}

void genOperator(struct node* p, int n, int flag)
{
    for(int i=0;i     {
        switch(flag & 1)
        {
            case 0:
                p[i].op='+';
                break;
            case 1:
                p[i].op='-';
                break;
        }
        flag=flag>>1;
    }

}

int Find(struct node* p, int n, int sum)
{
    int total=0;
    int res=0;
    memset(p, 0, 9*sizeof(struct node));

    while(p[n].digits<9)
    {
        if(genNumber(p, n, 9)==false)
            continue;

        toNumbers(p, n);

        for(int j=0;j         {
            genOperator(p, n, j);
            res=Calc(p, n);

            if(res==sum)
            {
                for(int k=0;k                 {
                    printf("%c%d",p[k].op,p[k].num);
                }
                printf("=%d\n",res);

                total++;
            }
        }
    }

    return total;
}

int main()
{
    int sum;
    int total=0;
    struct node p[9];
    memset(p, 0, 9*sizeof(struct node));

    printf("Input the sum: ");
    scanf("%d",&sum);

    puts("Results:");

    for(int i=1;i<10;i++)
        total+=Find(p, i, sum);

    printf("Total is %d\n", total);
}

[ Last edited by nono2009 on 2010-10-30 at 15:19 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

sxlion811

金虫 (正式写手)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
resonant(金币+1):欢迎参与讨论:-) 2010-04-27 12:05
此贴甚好!

其实以前SAS是用C编写的,后来根据需要改用java编写的。
C是好多软件的源头。
开心努力一辈子
3楼2010-04-27 10:49:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 12 个回答

yalefield

金虫 (文坛精英)

老汉一枚

★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
resonant(金币+3):向专家学习:-) 围观还是需要水平的,哈哈 2010-04-27 12:05
CODE:
#include
#include
#include
#include
#include

struct node
{
    int num;
    char op;
    int digits;
};

int Calc(struct node* p, int n);
int myAtoi(char* begin, int length);
int Find(struct node* p, int n, int sum);
bool genNumber(struct node* p, int n, int r);
void toNumbers(struct node* p, int n);
void genOperator(struct node* p, int n, int flag);
void Print(struct node* p, int n);

char numbers[]="123456789";

int myAtoi(char* begin, int length)
{
    int result=0;
    char* r=malloc(length+1);
    r=strncpy(r,begin,length);
    r[length+1]=0;
    result=atoi(r);
    free(r);
    return result;
}
void toNumbers(struct node* p, int n)
{
    char* q=&numbers[0];

    for(int i=0;i     {
        p[i].num=myAtoi(q,p[i].digits);
        q+=p[i].digits;
    }
}

int Calc(struct node* p, int n)
{
    int sum=0;
    sum=p[0].num;
    if(p[0].op=='-')
        sum=0-sum;

    for(int i=1;i     {
        switch(p[i].op)
        {
            case '+':
                sum+=p[i].num;
                break;
            case '-':
                sum-=p[i].num;
                break;
            default:
                break;
        }
    }

    return sum;
}

bool genNumber(struct node* p, int n, int r)
{
    if(r<0)
    {
        p[n+2].digits++;
        p[n+1].digits=0;
        p[n].digits=0;
        return false;
    }

    if(n==0)
    {
        if(r==0)
            return false;
        p[n].digits=r;
        return true;
    }

    if(n==1)
    {
        p[n].digits++;
    }

    if(p[n].digits==0)
    {
        p[n].digits=1;
    }
    genNumber(p, n-1, r-p[n].digits);
}

void genOperator(struct node* p, int n, int flag)
{
    for(int i=0;i     {
        switch(flag & 1)
        {
            case 0:
                p[i].op='+';
                break;
            case 1:
                p[i].op='-';
                break;
        }
        flag=flag>>1;
    }

}

int Find(struct node* p, int n, int sum)
{
    int total=0;
    int res=0;
    memset(p, 0, 9*sizeof(struct node));

    while(p[n].digits<9)
    {
        if(genNumber(p, n, 9)==false)
            continue;

        toNumbers(p, n);

        for(int j=0;j         {
            genOperator(p, n, j);
            res=Calc(p, n);

            if(res==sum)
            {
                for(int k=0;k                 {
                    printf("%c%d",p[k].op,p[k].num);
                }
                printf("=%d\n",res);

                total++;
            }
        }
    }

    return total;
}

int main()
{
    int sum;
    int total=0;
    struct node p[9];
    memset(p, 0, 9*sizeof(struct node));

    printf("Input the sum: ");
    scanf("%d",&sum);

    puts("Results:");

    for(int i=1;i<10;i++)
        total+=Find(p, i, sum);

    printf("Total is %d\n", total);
}

2楼2010-04-27 10:48:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

yalefield

金虫 (文坛精英)

老汉一枚

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
resonant(金币+1):欢迎参与讨论:-) 2010-04-27 12:05
引用回帖:
Originally posted by sxlion811 at 2010-04-27 10:49:55:
C是好多软件的源头。

上有天,下有地
中间全靠这个C
加加减减咖啡苦
正则脚本难独立
4楼2010-04-27 10:58:55
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

holmescn

金虫 (正式写手)

呵呵,大家互相学习。

[ Last edited by holmescn on 2010-4-27 at 11:04 ]
5楼2010-04-27 11:03:12
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见