标题:函数返回的的数字有公母之分?
只看楼主
Raylastin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-6-23
结帖率:50%
已结贴  问题点数:20 回复次数:4 
函数返回的的数字有公母之分?
//每组数据包含一行字符串,即24点游戏的表达式树的先序遍历序列,输入‘#’代表空树
//对于每组数据,输出一行。如果不能得到24,输出“NO”。如果能得到24,按样例输出。
/*例如
+ + + 6 # # 6 # # 6 # # 6 # #
- - * 6 # # 6 # # 6 # # 6 # #
* * 1 # # 2 # # * 1 # # 2 # #
(((6+6)+6)+6)=24
(((6*6)-6)-6)=24
NO*/
//源码
#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
 
typedef struct BiTNOde
{
    char data[4];
    struct BiTNOde *lchild,*rchild;
} BiTNOde,*BiTree;
 
void CreateBiTree(BiTree &t)
{
    char ch[4];
    if(!(cin>>ch))
        exit(0);
    if(ch[0]=='#')
        t=NULL;
    else
    {
        t=new BiTNOde;
        strcpy(t->data,ch);
        CreateBiTree(t->lchild);
        CreateBiTree(t->rchild);
    }
}
void DestoryBi(BiTree &t)
{
    if(t)
    {
        DestoryBi(t->lchild);
        DestoryBi(t->rchild);
        delete t;
    }
}
 
void PrintTraverse(BiTree t)
{
    if(t)
    {
        if(t->lchild)
            cout<<'(';
        PrintTraverse(t->lchild);
        cout<<t->data;
        PrintTraverse(t->rchild);
        if(t->rchild)
            cout<<')';
    }
 
}
 
double Myatoi(char *s)//(用库给的不行,用自己的试试)
{
    double sum=0;
    for(int i=0;s[i]!='\0';i++)
        sum=sum*10+s[i]-'0';
    return sum;
}
 
double Answer(BiTree t)
{
    if(t)
    {
        if(!t->lchild) return Myatoi(t->data);
        double x=Answer(t->lchild);
        double y=Answer(t->rchild);
 
        switch (t->data[0])
        {
            case'+':cout<<"x+y="<<x+y<<endl;return x+y;
            case'-':cout<<"x-y="<<x-y<<endl;return x-y;
            case'*':cout<<"x*y="<<x*y<<endl;return x*y;
            case'/':cout<<"x/y="<<x/y<<endl;return x/y;
        }
    }
}
 
int main()
{
    while(1)
    {
        BiTree t;
        CreateBiTree(t);
        if((int)Answer(t)==24){
        PrintTraverse(t);
        cout<<"=24"<<endl;
        }
        else cout<<"NO"<<endl;
        DestoryBi(t);
    }
    return 0;
}
//  / 8 # # - 3 # # / 8 # # 3 # #   (这组数据过不了)
//  - * 5 # # 5 # # / 8 # # 8 # #
搜索更多相关主题的帖子: include data cout double return 
2019-06-24 06:50
Raylastin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-6-23
得分:0 
对了,case后面的输出是我用来测试每一次递归的数值的
2019-06-24 06:53
Raylastin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-6-23
得分:0 
。。。case被谷歌给翻译了
2019-06-24 06:53
Raylastin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-6-23
得分:0 
那个数据在double下是24,在int下是23,加了个0.00001就好了
2019-06-24 07:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:14 
代码中怎么会有 double 出现?

不知道题目是什么,我随手写的
#include <iostream>
#include <string>
using namespace std;

const char* PreOrder( const char* s, int& numerator, int& denominator, std::string& expr )
{
    int offset;
    sscanf( s, " %n", &offset );
    s += offset;
    char ch = *s;

    if( !ch )
        return NULL;

    if( ch>='0' && ch<='9' )
    {
        char c1, c2; int offset;
        if( 3!=sscanf(s,"%d %c %c%n",&numerator,&c1,&c2,&offset) || c1!='#' || c2!='#' )
            return NULL;
        denominator = 1;
        char tmp[20];
        sprintf( tmp, "%d", numerator );
        expr = tmp;
        return s+offset;
    }

    if( ch=='+' || ch=='-' || ch=='*' || ch=='/' )
    {
        ++s;
        int a, b, c, d; std::string left, right;
        s = PreOrder(s,a,b,left);
        if( !s )
            return NULL;
        s = PreOrder(s,c,d,right);
        if( !s )
            return NULL;
        switch( ch )
        {
        case '+':
            numerator = a*d + b*c;
            denominator = b*d;
            break;
        case '-':
            numerator = a*d - b*c;
            denominator = b*d;
            break;
        case '*':
            numerator = a*c;
            denominator = b*d;
            break;
        case '/':
            numerator = a*d;
            denominator = b*c;
            break;
        }
        expr = '(' + left + ch + right + ')';
        return s;
    }

    return NULL;
}


int main( void )
{
    for( string line; getline(cin,line) && !line.empty(); )
    {
        int numerator, denominator; std::string expr;
        if( PreOrder(line.c_str(),numerator,denominator,expr) && denominator*24==numerator )
            cout << expr << "=24\n";
        else
            cout << "NO\n";
    }
}

2019-06-24 10:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-495465-1-1.html




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

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