美图Cpp服务端,第二题蚂蚁爬杆问题求解!
首先上题目:
#include<stdlib.h> #include <stdio.h> #include <vector> #include <iostream> using namespace std; int GetMaxTime(double length,double speed,vector<double>ans_pos) { int *distance = new int[ans_pos.size()]; int maxLenth = 0; for (int i = 0; i < ans_pos.size(); i++) { //杆两端中最远的距离为蚂蚁爬行的最大距离 if (ans_pos[i] <= length / 2) { distance[i] = length - ans_pos[i]; } else { distance[i] = ans_pos[i]; } if (maxLenth < distance[i]) { maxLenth = distance[i]; } } return maxLenth / speed; } int GetMinTime(double length,double speed,vector<double>ans_pos) { int* distance = new int[ans_pos.size()]; int minLenth = ans_pos[0]; for (int i = 0; i < ans_pos.size(); i++) { //杆两端中最近的距离为蚂蚁爬行的最小距离 if (ans_pos[i] <= length / 2) { distance[i] = ans_pos[i]; } else { distance[i] =length- ans_pos[i]; } if (minLenth > distance[i]) { minLenth = distance[i]; } } return minLenth / speed; } int main() { int n; double length,v; double pos; vector<double>ans_pos; cin>>n>>v>>length; for(int i=0;i<n;i++) { cin>>pos; ans_pos.push_back(pos); } cout<<GetMinTime(length,v,ans_pos)<<" "<<GetMaxTime(length,v,ans_pos)<<endl;; }求解到底哪里错了 就A了25!!!!