| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 467 人关注过本帖
标题:求一段算24点代码的解释
只看楼主 加入收藏
digimonx
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-9
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求一段算24点代码的解释
#include <stdio.h>
#include <string.h>

int  c24;
char expr3[80];
char expr4[80];

void count2( int a, int b )
{
    if ( a + b == 24 )
    {
        c24 ++;
        printf( "%s\t%s\t%d+%d=24\n", expr4, expr3, a, b );
    }

    if ( a * b == 24 )
    {
        c24 ++;
        printf( "%s\t%s\t%d*%d=24\n", expr4, expr3, a, b );
    }

    if ( a - b == 24 )
    {
        c24 ++;
        printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, a, b );
    }

    if ( b - a == 24 )
    {
        c24 ++;
        printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, b, a );
    }

    if ( (b!=0) && (a==24*b) )
    {
        c24 ++;
        printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, a, b );
    }

    if ( (a!=0) && (b==24*a) )
    {
        c24 ++;
        printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, b, a );
    }

}

void  count3( int a, int b, int c )
{
    int dd[3];
    int i,j,k,nd;
    dd[0] = a;
    dd[1] = b;
    dd[2] = c;
    for ( i = 0; i < 3; i++ )
    for ( j = i+1; j < 3; j++ )
    {
        k = 3 - i - j;

        nd = dd[i] + dd[j];
        sprintf( expr3, "%d+%d=%d", dd[i], dd[j], nd );
        count2( nd, dd[k] );

        nd = dd[i] * dd[j];
        sprintf( expr3, "%d*%d=%d", dd[i], dd[j], nd );
        count2( nd, dd[k] );

        if ( dd[i]>dd[j] )
        {
            nd = dd[i] - dd[j];
            sprintf( expr3, "%d-%d=%d", dd[i], dd[j], nd );
        }
        else
        {
            nd = dd[j] - dd[i];
            sprintf( expr3, "%d-%d=%d", dd[j], dd[i], nd );
        }
        count2( nd, dd[k] );

        if ( (dd[j]!=0) && (dd[i]%dd[j]==0) )
        {
            nd = dd[i] / dd[j];
            sprintf( expr3, "%d/%d=%d", dd[i], dd[j], nd );
            count2( nd, dd[k] );
        }
        else if ( (dd[i]!=0) && (dd[j]%dd[i]==0) )
        {
            nd = dd[j] / dd[i];
            sprintf( expr3, "%d/%d=%d", dd[j], dd[i], nd );
            count2( nd, dd[k] );
        }
    }
}


void  count4( int a, int b, int c, int d )
{
    int dd[4];//用来储存输入的abcd
    int i,j,k,m,nd;
    dd[0] = a;
    dd[1] = b;
    dd[2] = c;
    dd[3] = d;
    for ( i = 0; i < 4; i++ )
    for ( j = i+1; j < 4; j++ )
    {
        for ( k = 0; (k==i) || (k==j); k++ ) ;
        m = 6 - i - j - k;

        nd = dd[i] + dd[j];
        sprintf( expr4, "%d+%d=%d", dd[i], dd[j], nd );
        count3( nd, dd[k], dd[m] );

        nd = dd[i] * dd[j];
        sprintf( expr4, "%d*%d=%d", dd[i], dd[j], nd );
        count3( nd, dd[k], dd[m] );

        if ( dd[i]>dd[j] )
        {
            nd = dd[i] - dd[j];
            sprintf( expr4, "%d-%d=%d", dd[i], dd[j], nd );
        }
        else
        {
            nd = dd[j] - dd[i];
            sprintf( expr4, "%d-%d=%d", dd[j], dd[i], nd );
        }
        count3( nd, dd[k], dd[m] );

        if ( (dd[j]!=0) && (dd[i]%dd[j]==0) )
        {
            nd = dd[i] / dd[j];
            sprintf( expr4, "%d/%d=%d", dd[i], dd[j], nd );
            count3( nd, dd[k], dd[m] );
        }
        else if ( (dd[i]!=0) && (dd[j]%dd[i]==0) )
        {
            nd = dd[j] / dd[i];
            sprintf( expr4, "%d/%d=%d", dd[j], dd[i], nd );
            count3( nd, dd[k], dd[m] );
        }
    }
}

main()
{
    int a,b,c,d;
    printf( "Input 4 Number for Count 24: " );
    scanf( "%d%d%d%d", &a, &b, &c, &d );
    c24 = 0;
    count4( a, b, c, d );
    if ( c24==0 )
        printf( "No Answer\n" );
    /***/
    printf("End of C Program, Press Enter.\n");
    getchar();getchar();
    return 0;
}.
搜索更多相关主题的帖子: include 
2011-12-09 19:17
digimonx
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-12-9
收藏
得分:0 
求高手写个注释能让我看懂的,我是初学者,万分感谢啊
2011-12-09 19:27
贪狼oo
Rank: 2
等 级:论坛游民
帖 子:12
专家分:20
注 册:2011-7-21
收藏
得分:20 
你是南邮的吧
2011-12-10 00:34
快速回复:求一段算24点代码的解释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017543 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved