九度oj 1078,1113,1176 二叉树

本文详细介绍了如何通过前序遍历和中序遍历来求解后序遍历的问题,并给出了具体的C++实现代码。此外,还探讨了计算两点间二叉树高度及特定层级节点的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1078题目:

http://ac.jobdu.com/problem.php?pid=1078


大意是,告诉你前序遍历和中序遍历求后序遍历。

大致思路,通过前序遍历我们可以很容易知道这个二叉树的根节点知道,之后根据中序遍历,我们可以知道那些节点是左子树,哪些节点是右子树。

递归得解:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
 
using namespace std;
char cpre[30];
char cmed[30];
int med[30];
int len;
void theMedInthePre(){//计算中序遍历下的每个字符在前序输出中的对应的位置
    len=strlen(cpre);
    int k=0;
   for(int i=0;i<len;i++){
     for(int j=0;j<len;j++){
        if(cmed[j]==cpre[i]){
           med[j]=k;
           k++;
           break;
        }
     }
   }
//   cout<<"----------------------------\n"<<endl;
}
void post(int left,int right){
   if(right<left)
     return ;
   if(right==left){
     cout<<cmed[left];
     return ;
     }
    int min=30;
    int point=30;
    for(int i=left;i<=right;i++){
       if(med[i]<min){
         min=med[i];
         point=i;
       }
    }
    post(left,point-1);
    post(point+1,right);
    cout<<cmed[point];
}
 
int main(){
  while(scanf("%s%s",cpre,cmed)!=EOF){
     theMedInthePre();
     post(0,len-1);
     cout<<endl;
  }
 
}
 
/**************************************************************
    Problem: 1078
    User: zhouyudut
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/


1113题目:

http://ac.jobdu.com/problem.php?pid=1113

我的大致思路是,先求出这个两个点之间的高度,再进行计算。

虽然,思路很清晰,但是在实际编写过程中,总是WA。

究其原因,边界情况未考虑。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
 
using namespace std;
int length;//从n到m之间树的高度
long long int theLeftLeft;
long long int theRightRight;
void theLegnthOfLevel(long long int n,long long int m)//计算n到m之间树的高度
{
    length=0;
    long long int theUp=1;
    long long int tempn=n;
    while(tempn/2>0){
      theUp++;
      tempn/=2;
    }
    while(pow(2,theUp-1)<=m){
        length++;
        theUp++;
    }
}
void theNum(long long int n,long long int m){
  theLeftLeft=n;
  theRightRight=n;
  for(int i=1;i<length;i++){
    theLeftLeft=theLeftLeft<<1;
    theRightRight=(theLeftLeft<<1)-1;
  //  cout<<"theLeftLeft="<<theLeftLeft<<endl;
 //   cout<<"theRightRIght="<<theRightRight<<endl;
  }
  long long int num;
  num=pow(2,length-1)-1;
//  cout<<"length="<<length<<endl;
 // cout<<"num="<<num<<endl;
  if(theLeftLeft<=m && theRightRight>=m)
  {
      num+=m-theLeftLeft+1;
  }else if(theRightRight<m){
      num+=theRightRight-theLeftLeft+1;
  }
  cout<<num<<endl;
}
int main(){
   long long int n,m;
   while(1){
     scanf("%lld%lld",&n,&m);
     if(n==0 && m==0)
       return 0;
       theLegnthOfLevel(n,m);
       theNum(n,m);
   }
}
 
/**************************************************************
    Problem: 1113
    User: zhouyudut
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1608 kb
****************************************************************/

1176题目:

http://ac.jobdu.com/problem.php?pid=1176

还是那个问题,考虑不周全。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#include <queue>
#define ISYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
 
using namespace std;
int arr[1010];
int length;
int seek;
int main(){
   while(scanf("%d",&length)!=EOF){
     for(int i=1;i<=length;i++)
      cin>>arr[i];
     cin>>seek;
     int left=pow(2,seek-1);
     int right=pow(2,seek)-1;
     if(left>length){
     cout<<"EMPTY"<<endl;
     continue;
     }
     if(length<right)
     {
         right=length;
     }
        for(int i=left;i<right;i++){
            cout<<arr[i]<<' ';
        }
        cout<<arr[right]<<endl;
 
 
   }
}
 
/**************************************************************
    Problem: 1176
    User: zhouyudut
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1612 kb
****************************************************************/

求一个数的二进制的非符号位的最高位的1

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值