@[crush_blood](/user/992087) 您好,您的代码有以下几个问题:
1. 冒泡排序的 `if` 的条件后有一个不该出现的分号。
2. 累加答案时作为下标的 `x` 有可能超出 `n`,甚至超出数组范围 105。
by __PRO__ @ 2024-01-27 11:05:47
@[crush_blood](/user/992087) 其实用sort()更好
by zhangbomingpp @ 2024-01-27 11:09:21
@[__PRO__](/user/534597) 嗷嗷嗷谢谢!
by crush_blood @ 2024-01-27 15:10:58
@[zhangbomingpp](/user/945845) 刚开始用的是sort但是没过
by crush_blood @ 2024-01-27 15:11:26
@[__PRO__](/user/534597) 那那那咋改啊
by crush_blood @ 2024-01-27 15:13:51
@[crush_blood](/user/992087)
```cpp
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
double a[105],b[105],c[105],s;
int x,t,n;
int main()
{
cin>>n>>t;
for(int i=1;i<=n;i++)
{
cin>>a[i];cin>>b[i];
c[i]=b[i]/a[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(c[j]<c[j+1])
{
swap(a[j],a[j+1]);
swap(b[j],b[j+1]);
swap(c[j],c[j+1]);
}
}
}
while(t>0)
{
x++;
if(x>n)
break;
if(t>a[x])
{
s+=b[x];
t-=a[x];
}
else
{
s+=t*c[x];
break;
}
}
printf("%.2f",s);
}
```
by __PRO__ @ 2024-01-27 15:25:53
@[__PRO__](/user/534597) 感谢大佬!这题过了
by crush_blood @ 2024-01-27 15:31:58