题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560

注意点

这道题p、t的类型必须设置成double不然会有4个测试点错误

代码

  1. #include<vector>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cmath>
  5. using namespace std;
  6. const int maxn = 100010;
  7. int n;
  8. double p, t, sum = 0;
  9. struct Node{
  10. float amount;
  11. vector<int> child;
  12. }node[maxn];
  13. void DFS(int root, int count){
  14. if(node[root].child.size() == 0){
  15. sum += p * node[root].amount * pow(1 + t, count);
  16. return;
  17. }
  18. for(int i = 0; i < node[root].child.size(); i++){
  19. DFS(node[root].child[i], count + 1);
  20. }
  21. }
  22. int main(){
  23. //n是成员人数,p是基本价格,t是每层中间商的多收价格
  24. int child_num, child_temp, amount_temp;
  25. scanf("%d%lf%lf", &n, &p, &t);
  26. t /= 100;
  27. for(int i = 0; i < n; i++){
  28. scanf("%d", &child_num);
  29. if(child_num != 0){
  30. for(int j = 0; j < child_num; j++){
  31. scanf("%d", &child_temp);
  32. node[i].child.push_back(child_temp);
  33. node[i].amount = 0;
  34. }
  35. } else {
  36. scanf("%f", &node[i].amount);
  37. }
  38. }
  39. DFS(0, 0);
  40. printf("%.1f", sum);
  41. return 0;
  42. }