PAT 甲 1090 Highest Price in Supply Chain

2022.1.28 练习 PAT甲 1090 Highest Price in Supply Chain (原题链接)

段错误代码:(不明白为什么段错误,呜呜,求大佬指正)

#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE=100010;
int n;
double p,r;
int hashtable[MAX_SIZE]={0};

struct node
{
    vector<int> child;
    double price;
    int high;
}Node[MAX_SIZE];

void DFS(int index,int high)
{
    hashtable[high]++;
    Node[index].high=high;
    Node[index].price=p*pow((1+r),high);
    for(unsigned int i=0;i<Node[index].child.size();i++)
    {
        DFS(Node[index].child[i],high+1);
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n>>p>>r;
    r=r/100;
    int root=-1;
    for(int i=0;i<n;i++)
    {
        int tmp;
        cin>>tmp;
        Node[tmp].child.push_back(i);
        if(tmp==-1)
            root=i;
    }
    DFS(root,0);
    double max_p=-1;
    int res=-1;
    for(int i=0;i<n;i++)
    {
        if(Node[i].price>max_p)
        {
            max_p=Node[i].price;
            res=hashtable[Node[i].high];
        }
    }
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<max_p<<" "<<res;
    return 0;
}

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE=100010;
int n;
double p,r;

vector<int> child[MAX_SIZE];

int max_high=0,num=0;
void DFS(int index,int high)
{
    if(child[index].size()==0)
    {
        if(high>max_high)
        {
            max_high=high;
            num=1;//重置最大深度的叶节点个数为1
        }
        else if(high==max_high)
        {
            num++;
        }
        return;
    }
    for(unsigned int i=0;i<child[index].size();i++)
    {
        DFS(child[index][i],high+1);
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n>>p>>r;
    r=r/100;
    int root=-1;
    for(int i=0;i<n;i++)
    {
        int tmp;
        cin>>tmp;
        if(tmp!=-1)
            child[tmp].push_back(i);
        else
            root=i;
    }
    DFS(root,0);
    cout<<setiosflags(ios::fixed)<<setprecision(2)<<p*pow(1+r,max_high)<<" "<<num;
    return 0;
}

我就不懂了,我感觉这两个代码一样的嘛,为啥,哎,心痛,好痛苦,追求AC的路上,我还是渣渣。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值