全班都进前50的屑 • 2年前
int main() {
int n=10;
int a[n],i,cw,t;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(cw=0;cw<n-1;cw++)
{
for(i=0;i<n-1-cw;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
评论:
using namespace std;
int main() {
int bag, n, w[101], v[101], i, j, m = 0, val = 0;
double k[1000];
cin >> bag >> n;
for (i = 0; i < n; i++) {
cin >> w[i] >> v[i];
k[i] = 1.0 * v[i] / w[i];
}
for ( i = 0; i < n - 1; i++) {
for ( j = 0; j < n - 1 - i; j++) {
if (k[j] < k[j + 1]) {
int t1 = k[j];
k[j] = k[j + 1];
k[j + 1] = t1;
int t2 = w[i];
w[j] = w[j + 1];
w[j + 1] = t2;
int t3 = v[j];
v[j] = v[j + 1];
v[j + 1] = t3;
}
}
}
for (i = 0; i < n; i++) {
if (m + w[i] < bag) {
m = m + w[i];
val += v[i];
} else {
int t = bag - m;
m += t;
val = val + t * k[i];
}
}
cout << val;
return 0;
}
请先登录,才能进行评论