24小时热门版块排行榜    

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

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的回帖
查看全部 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的回帖

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的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 324分 085600材料化工求调剂 +4 llllkkkhh 2026-03-18 4/200 2026-03-21 01:24 by JourneyLucky
[考研] 材料专业求调剂 +6 hanamiko 2026-03-18 6/300 2026-03-21 00:24 by JourneyLucky
[考研] 294求调剂材料与化工专硕 +15 陌の森林 2026-03-18 15/750 2026-03-20 23:28 by JourneyLucky
[考研] 321求调剂 +9 何润采123 2026-03-18 11/550 2026-03-20 23:19 by JourneyLucky
[考研] 药学383 求调剂 +3 药学chy 2026-03-15 5/250 2026-03-20 22:11 by 云游重阳
[考研] 一志愿北京化工大学0703化学318分,有科研经历,求调剂 +4 一瓶苯甲酸 2026-03-14 4/200 2026-03-20 20:36 by fen_rao
[考研] 261求B区调剂,科研经历丰富 +3 牛奶很忙 2026-03-20 4/200 2026-03-20 19:34 by JourneyLucky
[考研] 材料与化工专硕调剂 +7 heming3743 2026-03-16 7/350 2026-03-20 19:31 by zhukairuo
[论文投稿] 申请回稿延期一个月,编辑同意了。但系统上的时间没变,给编辑又写邮件了,没回复 10+3 wangf9518 2026-03-17 4/200 2026-03-19 23:55 by babero
[考研] 一志愿西安交通大学材料工程专业 282分求调剂 +5 枫桥ZL 2026-03-18 7/350 2026-03-19 14:52 by 功夫疯狂
[考研] 材料工程专硕调剂 +5 204818@lcx 2026-03-17 6/300 2026-03-18 22:55 by 204818@lcx
[考研] 【同济软件】软件(085405)考研求调剂 +3 2026eternal 2026-03-18 3/150 2026-03-18 19:09 by 搏击518
[考研] 08工科 320总分 求调剂 +5 梨花珞晚风 2026-03-17 5/250 2026-03-18 14:49 by haxia
[考研] 070300化学319求调剂 +6 锦鲤0909 2026-03-17 6/300 2026-03-18 13:22 by Iveryant
[考研] 考研求调剂 +3 橘颂. 2026-03-17 4/200 2026-03-17 21:43 by 有只狸奴
[考研] 材料专硕326求调剂 +6 墨煜姒莘 2026-03-15 7/350 2026-03-17 17:10 by ruiyingmiao
[考研] 一志愿211 0703方向310分求调剂 +3 努力奋斗112 2026-03-15 3/150 2026-03-16 16:44 by houyaoxu
[考研] 070303 总分349求调剂 +3 LJY9966 2026-03-15 5/250 2026-03-16 14:24 by xwxstudy
[考研] 297一志愿上交085600求调剂 +5 指尖八千里 2026-03-14 5/250 2026-03-14 17:26 by a不易
[考研] 一志愿哈工大材料324分求调剂 +5 闫旭东 2026-03-14 5/250 2026-03-14 14:53 by 木瓜膏
信息提示
请填处理意见