| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1392 人关注过本帖
标题:函数返回的的数字有公母之分?
取消只看楼主 加入收藏
Raylastin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-6-23
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
函数返回的的数字有公母之分?
//每组数据包含一行字符串,即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
快速回复:函数返回的的数字有公母之分?
数据加载中...
 
   



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

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