24СʱÈÈÃŰæ¿éÅÅÐаñ    

²é¿´: 2452  |  »Ø¸´: 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 µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[»ù½ðÉêÇë] 2026¹ú×ÔÈ»º¯ÆÀ·Ñµ½ÕË +17 ÑòÑü°å 2026-08-21 18/900 2026-08-24 12:03 by iaeyuan
[»ù½ðÉêÇë] ½¨Òé»ù½ð·¢²¼Ìáǰ¸ø³öÃ÷È·µÄʱ¼äµã +12 kulium 2026-08-21 15/750 2026-08-24 12:02 by iaeyuan
[»ù½ðÉêÇë] ÅóÓÑȦ¿´µ½µÄ +7 wangzilk 2026-08-18 9/450 2026-08-24 10:50 by cmrandy
[»ù½ðÉêÇë] ÄÜ·ñÍ˳ö²ÎÓëµÄÃæÉÏÏîÄ¿½â³ýÏÞÏî +11 koalala 2026-08-24 13/650 2026-08-24 10:49 by anjeeshine
[»ù½ðÉêÇë] ·Å°ñǰµÄ²»µ­¶¨ 20+4 snowwithsea 2026-08-19 17/850 2026-08-24 10:20 by echo8914667
[»ù½ðÉêÇë] ¿ÆÑй¶ùÌ«ÄÑÁË +18 ÎÒ4´ó°×²Ë 2026-08-20 19/950 2026-08-24 09:47 by ¿­¶÷¹ãÊ¢´ß»¯·ÖÎ
[»ù½ðÉêÇë] 2026ÄêµÄ¹ú¼ÒÉç¿Æ»ù½ðÏîĿͨѶÆÀÉóµÄйæÔòÓëж¯Ïò¡¢ÐÂÌôÕ½ +4 process2012 2026-08-23 5/250 2026-08-23 19:58 by jurkat.1640
[»ù½ðÉêÇë] ½ñÌì·Å°ñÂ𣿠+15 ²¼²¼ºÍÒ»¶þ 2026-08-19 16/800 2026-08-23 09:55 by ÕÅ´ºÉú
[»ù½ðÉêÇë] 93BebMhtakhǰºó11λ¿ªÍ·¶¼ÊÇ´óд +7 ÇÒÌý»¢Ð¥ 2026-08-17 8/400 2026-08-22 21:55 by ҽѧÀÏÄк¢
[»ù½ðÉêÇë] filecode£¬4¸öjtjcÁË +13 ziyangfang 2026-08-19 16/800 2026-08-22 17:08 by WH3796
[»ù½ðÉêÇë] ÈËÆø²»ÐÐÁË +8 fansofjerry 2026-08-21 8/400 2026-08-22 16:30 by zyqchem
[»ù½ðÉêÇë] ½ñÈÕ²»·Å°ñ£¿Íø´«¹ú×ÔȻԤ¼Æ 8 Ô 27 Èտɲé½á¹û +16 ҽѧÀÏÄк¢ 2026-08-20 20/1000 2026-08-21 21:13 by Ldrop2023
[»ù½ðÉêÇë] ¿´À´½ñÌì²»»á·Å°ñÁË£¿ +8 chengyan1220 2026-08-21 11/550 2026-08-21 17:52 by dcqxinyang
[»ù½ðÉêÇë] ʱ¼ä´ÁÓÖ±äÁË +13 wuchongjun 2026-08-20 19/950 2026-08-21 17:21 by ×Ïɼ´¼
[»ù½ðÉêÇë] ÎÒÃæÉÏÍêµ°ÁË +7 ÇÒÌý»¢Ð¥ 2026-08-20 8/400 2026-08-21 12:31 by ¿á¿áÄ«¾µ
[»ù½ðÉêÇë] Ó¦¸ÃÊÇÏÂÖÜÈý26ÈÕ¹«²¼Á˰ɣ¿ +4 ¹þ¹þ¸ò£¿ 2026-08-21 4/200 2026-08-21 10:58 by Vivilian
[»ù½ðÉêÇë] ½ñÌì·Å°ñûϷÁ衃 +9 yuleib84 2026-08-19 11/550 2026-08-21 10:06 by gltch
[»ù½ðÉêÇë] »ù½ð°¡»ù½ð +4 longfie172 2026-08-20 4/200 2026-08-21 08:58 by mark mao
[»ù½ðÉêÇë] Ã÷Ìì·Å°ñ£¿ +5 Shxjjxjkx 2026-08-18 5/250 2026-08-18 18:14 by -´ó´ó´ó´ó´ó-
[»ù½ðÉêÇë] ½ñÌìά»¤ÏµÍ³Î¬»¤ ×£ËùÓÐÈË ¸ßÖÐ +8 gjjjzhong 2026-08-18 9/450 2026-08-18 13:01 by ¼ÒÓëÔ¶·½
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û