24小时热门版块排行榜    

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

holmescn

金虫 (正式写手)

呵呵,大家互相学习。

[ Last edited by holmescn on 2010-4-27 at 11:04 ]
5楼2010-04-27 11:03:12
已阅   回复此楼   关注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的回帖
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 08工科 320总分 求调剂 +6 梨花珞晚风 2026-03-17 6/300 2026-03-21 03:40 by JourneyLucky
[考研] 初始318分求调剂(有工作经验) +3 1911236844 2026-03-17 3/150 2026-03-21 02:33 by JourneyLucky
[考研] 307求调剂 +10 冷笙123 2026-03-17 10/500 2026-03-21 01:54 by JourneyLucky
[考研] 304求调剂 +6 曼殊2266 2026-03-18 6/300 2026-03-21 00:32 by JourneyLucky
[考研] 290求调剂 +7 ^O^乜 2026-03-19 7/350 2026-03-20 21:43 by JourneyLucky
[考研] 353求调剂 +3 拉钩不许变 2026-03-20 3/150 2026-03-20 19:56 by JourneyLucky
[考研] 一志愿吉林大学材料学硕321求调剂 +11 Ymlll 2026-03-18 15/750 2026-03-20 19:40 by 丁丁*
[考研] 298-一志愿中国农业大学-求调剂 +9 手机用户 2026-03-17 9/450 2026-03-20 14:24 by 无懈可击111
[考研] 0854可跨调剂,一作一项核心论文五项专利,省、国级证书40+数一英一287 +8 小李0854 2026-03-16 8/400 2026-03-18 14:35 by 搏击518
[考研] 收复试调剂生 +4 雨后秋荷 2026-03-18 4/200 2026-03-18 14:16 by elevennnne
[考研] 268求调剂 +6 简单点0 2026-03-17 6/300 2026-03-18 09:04 by 无际的草原
[硕博家园] 湖北工业大学 生命科学与健康学院-课题组招收2026级食品/生物方向硕士 +3 1喜春8 2026-03-17 5/250 2026-03-17 17:18 by ber川cool子
[考研] 考研调剂 +3 淇ya_~ 2026-03-17 5/250 2026-03-17 09:25 by Winj1e
[考研] [导师推荐]西南科技大学国防/材料导师推荐 +3 尖角小荷 2026-03-16 6/300 2026-03-16 23:21 by 尖角小荷
[考研] 304求调剂 +5 素年祭语 2026-03-15 5/250 2026-03-16 17:00 by 我的船我的海
[考研] 070303 总分349求调剂 +3 LJY9966 2026-03-15 5/250 2026-03-16 14:24 by xwxstudy
[考研] 0703化学调剂 290分有科研经历,论文在投 +7 腻腻gk 2026-03-14 7/350 2026-03-16 10:12 by houyaoxu
[考研] 327求调剂 +6 拾光任染 2026-03-15 11/550 2026-03-15 22:47 by 拾光任染
[考研] 085601材料工程315分求调剂 +3 yang_0104 2026-03-15 3/150 2026-03-15 10:58 by peike
[考研] 297一志愿上交085600求调剂 +5 指尖八千里 2026-03-14 5/250 2026-03-14 17:26 by a不易
信息提示
请填处理意见