测试点2、3、5没过问题:网上调查之后发现,用float计算结果精度不够,改成double就好了

测试点6一开始超时了,当时代码:

#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
vector<int>father;
vector <int> is_retailer;
int times;double sum;
double price, rate;
void dps(int end,int now) {if (father[now] == -1) {sum += is_retailer[end] * price * pow(1 + rate / 100, times);return;}times++;dps(end, father[now]);
}
int main() {int N; cin >> N;cin >> price >> rate;father.resize(N,-1);is_retailer.resize(N,-1);for (int i = 0; i < N; i++) {int number; cin >> number;for (int j = 1; j <= number; j++) {int child; cin >> child;father[child] = i;}if (number == 0) cin >> is_retailer[i];}sum = 0;for (int i = 0; i < N; i++) {if (is_retailer[i] != -1) {times = 0;dps(i, i);}}printf("%.1lf", sum);return 0;
}

然后稍微看了一下这个链接的思路:PAT甲级1079_湛蓝的天空-CSDN博客

AC代码如下:

#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
vector<int>children;
vector <int> is_retailer;
vector<int>layer;
double sum,price, rate;
void update(int father) {if(children[father]==-1) return;layer[children[father]] = layer[father] + 1;update(children[father]);
}
int main() {int N; cin >> N;cin >> price >> rate;children.resize(N,-1);is_retailer.resize(N,-1);layer.resize(N, 0);for (int i = 0; i < N; i++) {int number; cin >> number;for (int j = 1; j <= number; j++) {int child; cin >> child;children[i] = child;layer[child] = layer[i] + 1;update(child);}if (number == 0) cin >> is_retailer[i];}sum = 0;for (int i = 0; i < N; i++) {if (is_retailer[i] != -1) sum += is_retailer[i] * price * pow(1 + rate / 100, layer[i]);}printf("%.1lf", sum);return 0;
}

相比改之前的代码,dps更新层数走过的路径更短,而改之前要把所有路走到头。

PAT甲级 1079(C++)相关推荐

  1. PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树、结点到根结点的距离、树形dp、记忆化搜索

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:下图是对样例的模拟图示,题目就是统计叶子结点卖出去的钱数.根据下图,我们第一步是建树,第二步是统计叶子结点到根结点的距离,然后才能知道 ...

  2. PAT甲级 -- 1079 Total Sales of Supply Chain (25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. PAT甲级1106 Lowest Price in Supply Chain:[C++题解]树、结点到根结点的距离、树形dp、记忆化搜索

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这道题是第三次做了. 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的 ...

  4. PAT甲级1090 Highest Price in Supply Chain:[C++题解]树、结点到根结点的距离、记忆化搜索、树形dp

    文章目录 题目分析 题目链接 题目分析 来源:acwing 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的距离.树形dp.记忆化搜索是 ...

  5. PAT甲级题目对应知识点分类梳理

    PAT甲级的106道题的知识点与对应的题号整理如下,便于做专项练习和巩固! 1.数据结构 可以用STL系列 栈:1051 堆:1098 队列:1014.1056 链表:1032.1052.1074.1 ...

  6. 【PAT甲级】字符串处理及进制转换专题

    目录 字符串处理 PAT甲级 1001 A+B Format (20 分) PAT甲级1005 Spell It Right (20 分) PAT甲级1035 Password (20 分) PAT甲 ...

  7. PAT甲级考试题库题目分类

    PAT甲级题目分类 水题 1136.1139.1143.1148 字符串处理 1001.1005.1035.1061.1073.1077.1082. 1108.1140.1152 模拟 1002.10 ...

  8. PAT甲级题目解析和知识点分类整理

    转载请注明出处 个人博客:https://maxusun.github.io/ 今天整理电脑,发现了去年为了考研浙大计算机整理的PAT资料.现在考研已经尘埃落定.想到当时盲目刷题浪费了好多时间,在这里 ...

  9. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

最新文章

  1. 学习笔记:Model Diagnostics-模型诊断(线性回归)
  2. openstack常用命令及控制节点端口一览
  3. crypto++使用DES加密(ECB pkcs7padding )
  4. 用Unity盖房子(一):《勇者斗恶龙:建造者2》游戏功能的猜想
  5. java.util中,util是什么意思
  6. hadoop2.2.0 分布式存储hdfs完全分布式搭建及功能测试记录(一)----架构及原理介绍...
  7. linux gradle仓库位置,如何在Android Studio中使用Gradle发布项目至Jcenter仓库
  8. 使用PackageManager获得应用(包)信息
  9. SpringBoot的自定义配置方法一,通过自定义配置文件
  10. 布客·ApacheCN 编程/后端/大数据/人工智能学习资源 2020.6
  11. Manjaro - Pacman命令详解
  12. 算法学习总结(2)——温故十大经典排序算法
  13. 004/160 CrackMe ajj CKme
  14. 网络(Network)
  15. 基于Flink的实时数据消费应用、功能质量保障方法
  16. C++新特性:override
  17. 混合波束成形|基础:深入浅出5G,毫米波,大规模MIMO与波束赋形
  18. 使用MMA解决拓扑优化问题的并行框架
  19. VBoxManage.exe: error: Failed to create the host-only adapter
  20. 我实现的第一个算法----归并排序

热门文章

  1. 华中科技大学计算机组成原理实验,单总线CPU设计(变/定长指令周期3级时序)(HUST),logisim,educoder
  2. 抖音火爆的早安推送在线版,新功能速递,支持推送时间自定义,添加生日日期计算
  3. 对象存储-docker学习
  4. 《云计算架构技术与实践》连载(1)1.1 云计算的基础概念与架构
  5. 个人HTML期末大作业~ 个人网页(HTML+CSS)6页面带下拉特效~简单带表格带设计说明 ~学生网页设计作业源码...
  6. 树莓派——9、IO操控代码编程
  7. DTFT, DFT, FFT 的区别、联系和特点
  8. (开源) 从零实现一款图片编辑器
  9. 在校外使用校园网服务
  10. 智能牙刷不再是简单的3C产品,更应该是智能口腔管家