24小时热门版块排行榜    

查看: 2202  |  回复: 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的回帖

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的回帖

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的回帖

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的回帖

snoopyzhao

至尊木虫 (职业作家)

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

其实以前SAS是用C编写的,后来根据需要改用java编写的。
C是好多软件的源头。

你确定 SAS 是用 C 编写的?SAS 出生的时候还没有 C 吧?

另外,现在的 SAS 也不会是 Java 写的,除非你说的是界面,这个我就不清楚了……
6楼2010-04-27 11:52:22
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

resonant(金币+1):欢迎新虫,请勿纯表顶贴(*_*) 2010-04-27 13:22
7楼2010-04-27 13:03:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

cangcang3683

金虫 (小有名气)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
resonant(金币+1):欢迎围观讨论,我想你或许可以看看这个帖子就大致明白了。http://emuch.net/bbs/viewthread.php?tid=1981811&fpage=1 2010-04-27 13:53
虽然看不懂,还是顶上,辛苦了楼主
8楼2010-04-27 13:36:49
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

sxlion811

金虫 (正式写手)


小木虫(金币+0.5):给个红包,谢谢回帖交流
引用回帖:
Originally posted by snoopyzhao at 2010-04-27 11:52:22:


你确定 SAS 是用 C 编写的?SAS 出生的时候还没有 C 吧?

另外,现在的 SAS 也不会是 Java 写的,除非你说的是界面,这个我就不清楚了……

汇编最NB。
开心努力一辈子
9楼2010-04-27 17:58:31
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

天雪小木虫

金虫 (初入文坛)

强人

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
jjdg(金币+1):欢迎新虫 2010-05-03 13:41
需要太高深的逻辑了...
10楼2010-05-03 12:25:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 holmescn 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 求调剂 +4 Mqqqqqq 2026-03-19 4/200 2026-03-20 14:15 by 星空星月
[基金申请] 学校已经提交到NSFC,还能修改吗? 40+3 babangida 2026-03-19 7/350 2026-03-20 14:13 by hejicker
[考研] 279分求调剂 一志愿211 +9 chaojifeixia 2026-03-19 10/500 2026-03-20 12:29 by lature00
[考研] 工科材料085601 279求调剂 +6 困于星晨 2026-03-17 8/400 2026-03-20 11:24 by kkcoco25
[考研] 一志愿中国海洋大学,生物学,301分,求调剂 +5 1孙悟空 2026-03-17 6/300 2026-03-19 23:46 by zcl123
[考研] 320求调剂0856 +3 不想起名字112 2026-03-19 3/150 2026-03-19 22:53 by 学员8dgXkO
[考研] 288求调剂 +15 于海海海海 2026-03-19 15/750 2026-03-19 22:41 by 学员8dgXkO
[考研] 生物学调剂招人!!! +3 山海天岚 2026-03-17 4/200 2026-03-19 21:34 by 怎么释怀
[考研] 材料与化工求调剂 +7 为学666 2026-03-16 7/350 2026-03-19 14:48 by 尽舜尧1
[考研] 330求调剂 +3 小材化本科 2026-03-18 3/150 2026-03-18 21:55 by 无懈可击111
[考研] 328求调剂,英语六级551,有科研经历 +3 生物工程调剂 2026-03-17 7/350 2026-03-18 20:41 by Wangjingyue
[考研] 材料专硕306英一数二 +10 z1z2z3879 2026-03-16 13/650 2026-03-18 14:20 by 007_lilei
[考研] 材料,纺织,生物(0856、0710),化学招生啦 +3 Eember. 2026-03-17 9/450 2026-03-18 10:28 by Eember.
[考研] 环境工程调剂 +8 大可digkids 2026-03-16 8/400 2026-03-18 09:36 by zhukairuo
[考研] 268求调剂 +6 简单点0 2026-03-17 6/300 2026-03-18 09:04 by 无际的草原
[考研] 293求调剂 +11 zjl的号 2026-03-16 16/800 2026-03-18 08:10 by zhukairuo
[考研] 301求调剂 +4 A_JiXing 2026-03-16 4/200 2026-03-17 17:32 by ruiyingmiao
[考研] 中科大材料与化工319求调剂 +3 孟鑫材料 2026-03-14 3/150 2026-03-14 20:10 by ms629
[考研] 一志愿哈工大材料324分求调剂 +5 闫旭东 2026-03-14 5/250 2026-03-14 14:53 by 木瓜膏
[考研] 311求调剂 +3 冬十三 2026-03-13 3/150 2026-03-13 20:41 by JourneyLucky
信息提示
请填处理意见