当前位置: 代码迷 >> 综合 >> PATA 1079 Total Sales of Supply Chain
  详细解决方案

PATA 1079 Total Sales of Supply Chain

热度:94   发布时间:2024-02-09 14:39:57.0

在这里插入图片描述
在这里插入图片描述

题记

思路
这道题读懂题就好做多了。大概意思就是每个结点的销售单价都比父结点的单价贵%r,计算最后零售店的销售额。首先建立起一棵树,这棵树不是二叉树,所以需要用vector来保存孩子结点的下标。之后再找到每个叶子结点的高度(用来计算单价)
具体步骤
1.建立结点的数据结构,读入数据,在读入数据的同时建立起二叉树
2.进行先根遍历,根节点的深度是0,往下每次一层深度加一
3.根据叶子结点深度计算各自的销售额并且相加
易错点
1.不要在读入数据的时候简单的认为孩子结点的深度等于当前结点深度加一就急着更新记录数据,其实这样做是错的,因为当前结点的深度可能还不知道。
2.用double不要用float


代码如下

#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
using namespace std;const int Maxn=100010;//输入
int n;
double P,r;struct Node{int height=0;//存储深度(根结点为0)bool isretailer=false;//是否是零售店,若是为truedouble num=0.0;//货物数量(retailer为true才有效)vector<int> child;//存储孩子的下标
}node[Maxn];void pre(int root,int height){//先根遍历找出每个结点的深度node[root].height=height;for(int i=0;i<node[root].child.size();i++)pre(node[root].child[i],height+1);
}int main()
{double sum=0;//记录总的营销额scanf("%d %lf %lf",&n,&P,&r);r=r/100;for(int i=0;i<n;i++){int k;scanf("%d",&k);if(k==0){//这个结点是零售商node[i].isretailer=true;scanf("%lf",&node[i].num);}else{node[i].isretailer=false;int index_child;for(int j=0;j<k;j++){scanf("%d",&index_child);node[i].child.push_back(index_child);}}}pre(0,0);for(int i=0;i<n;i++){if(node[i].isretailer){sum+=P*node[i].num*pow(1+r,node[i].height);}}printf("%.1f\n",sum);return 0;
}
  相关解决方案