为甚么错了?
查看原帖
为甚么错了?
976369
Zyh110113楼主2023/7/7 16:53
#include <cstdio>
using namespace std;
int h[110];
double cost[110];
int main()
{
    int n,t,i,j,temp;
	double ans=0;
    double m;
    cin>>n>>t;
    for(i=1;i<=n;i++)
    {
      cin>>h[i];
	  cin>>m;
      cost[i]=m/h[i];
    }
    for(i=n;i>=2;i--)
      for(j=1;j<=i-1;j++)
        if(cost[j]<cost[j+1])
        {
          temp=cost[j];
          cost[j]=cost[j+1];
          cost[j+1]=temp;
          temp=h[j];
          h[j]=h[j+1];
          h[j+1]=temp;
        }
    for(i=1;i<=n;i++)
    {
      if(t==0)
        break;
      else
      {
        if(t-h[i]>-0.000001)
        {
          ans+=t*cost[i];
          t=0;
        }
        else
        {
          ans+=h[i]*cost[i];
          t-=h[i];
        }
      }
    }
     printf("%.2lf",ans);
    return 0;
}
2023/7/7 16:53
加载中...