p,r用double类型,pat多题如果用float会WA
AC代码
#include <vector> #include <cstdio> #include <cmath> using namespace std; int main(){ int n; double p,r; scanf("%d %lf %lf",&n,&p,&r); r /= 100.0; vector<int> s; vector<int> ret(n); vector<bool> vs(n,false); for(int i = 0;i < n;i++){ int tmp; scanf("%d",&tmp); if(tmp == -1){ ret[i] = 0; vs[i] = true; } s.push_back(tmp); } int max(0); for(int i = 0;i < s.size();i++){ if(vs[i]) continue; else{ vector<int> tmp; int id(i); while(id != -1){ tmp.push_back(id); id = s[id]; if(vs[id]) break; } for(int j = 0;j < tmp.size();j++){ vs[tmp[j]] = true; ret[tmp[j]] = ret[id] + tmp.size() - j; } if(ret[tmp[0]] > max) max = ret[tmp[0]]; } } int count(0); for(int i = 0;i < ret.size();i++){ if(ret[i] == max) count++; } printf("%.2lf %d\n",p * pow(1+r,max),count); return 0; }