1079. Total Sales of Supply Chain (25)
来源:互联网 发布:周立功单片机同类公司 编辑:程序博客网 时间:2024/06/07 03:45
题目详情:https://www.patest.cn/contests/pat-a-practise/1079
提交情况:
代码:
#include <iostream>#include <vector>#include <math.h> using namespace std;#define Max 100010//depths[]存储各节点的深度 //isRoot[]存储各个节点是否为根节点,如果isRoot[i] != 0则代表节点i为叶子节点//depths[]存储各节点的深度 int n,visit[Max],isRoot[Max],depth,depths[Max];double rootPrice,percent;vector<int> child[Max];void DFS( int vertex,int depth ) //深度遍历,求各节点的深度 { visit[vertex] = 1; depths[vertex] = depth; for( int i=0;i<child[vertex].size();++i ) { int key = child[vertex][i]; if( visit[key] == 0 ) { ++depth; DFS(key,depth); --depth; } }}int main (){ cin>>n>>rootPrice>>percent; for( int i = 0;i<n;++i ) { int num,key,count; cin>>num; if( num == 0 ) //代表节点i为叶子节点 { cin>>count; isRoot[i] = count; } //非叶子节点。叶子节点一开始就不满足循环条件 for( int j = 0;j < num;++j ) { cin>>key; child[i].push_back(key); } } //DFS前的初始化 depth = 1; DFS(0,depth); double total = 0; for( int i=0;i<n;++i ) { if( isRoot[i] != 0 ) //代表为叶子节点 { total += rootPrice*pow(1+percent/100,depths[i]-1)*isRoot[i]; //rootPrice*pow(1+percent/100,depths[i]-1)求出各个叶节点的价格 //isRoot[i]是各个叶节点中产品的数量 } } printf("%.1lf\n",total); return 0;}
用vector作为树的存储结构,DFS()遍历求出各节点的深度,然后根据各个叶节点的价格乘以数量求出总的销售额。是不是说的太简单了?本题和 1090题类似。
0 0
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- 1079. Total Sales of Supply Chain (25)
- web前端训练营第二天
- 【Oracle】Oracle引起服务器宕机,查询定位是哪条SQL语句引起的
- Java互联网架构师视频课程 分布式系统架构 Dubbo ActiveMQ Netty
- 第六章 PX4-Pixhawk-Sensors解析
- C++基础之智能指针
- 1079. Total Sales of Supply Chain (25)
- (逆向)angr 执行二进制函数
- 51nod 1101 换零钱
- CSU 1867 John and Health rate
- hibernate中联合主键的使用
- 选择语句 while 和do..while
- gcc-stack-protector机制
- Android屏幕适配方案http://blog.csdn.net/lmj623565791/article/details/45460089
- React入门学习