#include<bits/stdc++.h> using namespace std; int main() { long long n,l,h; cin>>n>>l>>h; int a[n],b[n],c[n]; int p=0,a1[n],b1[n],c1[n],t=0,t1=0,t2=0,a2[n],b2[n],c2[n],a3[n],b3[n],c3[n]; for(int i=0;i<n;i++) { cin>>a[i]>>b[i]>>c[i]; if(b[i]>=l&&c[i]>=l) { p++; if(b[i]>=h&&c[i]>=h) { a1[t++]=a[i]; b1[t++]=b[i]; c1[t++]=c[i]; } else if(b[i]>=h&&c[i]<h) { a2[t1++]=a[i]; b2[t1++]=b[i]; c2[t1++]=c[i]; } else if(b[i]<h&&c[i]>=h) { a3[t2++]=a[i]; b3[t2++]=b[i]; c3[t2++]=c[i]; } } } cout<<p<<endl; int y; for(int i=0;i<t;i++) { for(int j=0;j<t;j++) { if((b1[j]+c1[j])<(b1[j+1]+c1[j+1])) { y=a1[j]; a1[j]=a1[j+1]; a1[j+1]=y; y=b1[j]; b1[j]=b1[j+1]; b1[j+1]=y; y=c1[j]; c1[j]=c1[j+1]; c1[j+1]=y; } } } for(int i=0;i<t1;i++) { for(int j=0;j<t1;j++) { if((b2[j]+c2[j])<(b2[j+1]+c2[j+1])) { y=a2[j]; a2[j]=a2[j+1]; a2[j+1]=y; y=b2[j]; b2[j]=b2[j+1]; b2[j+1]=y; y=c2[j]; c2[j]=c2[j+1]; c2[j+1]=y; } } } for(int i=0;i<t2;i++) { for(int j=0;j<t2;j++) { if((b3[j]+c3[j])<(b3[j+1]+c3[j+1])) { y=a3[j]; a3[j]=a3[j+1]; a3[j+1]=y; y=b3[j]; b3[j]=b3[j+1]; b3[j+1]=y; y=c3[j]; c3[j]=c3[j+1]; c3[j+1]=y; } } } for(int i=0;i<t;i++) { cout<<a1[i]<<" "<<b1[i]<<" "<<c1[i]<<endl; } for(int i=0;i<t1;i++) { cout<<a2[i]<<" "<<b2[i]<<" "<<c2[i]<<endl; } for(int i=0;i<t2;i++) { cout<<a3[i]<<" "<<b3[i]<<" "<<c3[i]<<endl; } return 0; }改错
时间: 2023-11-27 09:05:29 浏览: 90
在排序时,数组下标应该是从0开始的。因此,第一个for循环中,j应该从0开始,最大值应该是t-2;第二个for循环和第三个for循环也需要修改。修改后的代码如下:
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,l,h;
cin>>n>>l>>h;
int a[n],b[n],c[n];
int p=0,a1[n],b1[n],c1[n],t=0,t1=0,t2=0,a2[n],b2[n],c2[n],a3[n],b3[n],c3[n];
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i]>>c[i];
if(b[i]>=l&&c[i]>=l)
{
p++;
if(b[i]>=h&&c[i]>=h)
{
a1[t]=a[i];
b1[t]=b[i];
c1[t]=c[i];
t++;
}
else if(b[i]>=h&&c[i]<h)
{
a2[t1]=a[i];
b2[t1]=b[i];
c2[t1]=c[i];
t1++;
}
else if(b[i]<h&&c[i]>=h)
{
a3[t2]=a[i];
b3[t2]=b[i];
c3[t2]=c[i];
t2++;
}
}
}
cout<<p<<endl;
int y;
for(int i=0;i<t-1;i++)
{
for(int j=0;j<t-1-i;j++)
{
if((b1[j]+c1[j])<(b1[j+1]+c1[j+1]))
{
y=a1[j];
a1[j]=a1[j+1];
a1[j+1]=y;
y=b1[j];
b1[j]=b1[j+1];
b1[j+1]=y;
y=c1[j];
c1[j]=c1[j+1];
c1[j+1]=y;
}
}
}
for(int i=0;i<t1-1;i++)
{
for(int j=0;j<t1-1-i;j++)
{
if((b2[j]+c2[j])<(b2[j+1]+c2[j+1]))
{
y=a2[j];
a2[j]=a2[j+1];
a2[j+1]=y;
y=b2[j];
b2[j]=b2[j+1];
b2[j+1]=y;
y=c2[j];
c2[j]=c2[j+1];
c2[j+1]=y;
}
}
}
for(int i=0;i<t2-1;i++)
{
for(int j=0;j<t2-1-i;j++)
{
if((b3[j]+c3[j])<(b3[j+1]+c3[j+1]))
{
y=a3[j];
a3[j]=a3[j+1];
a3[j+1]=y;
y=b3[j];
b3[j]=b3[j+1];
b3[j+1]=y;
y=c3[j];
c3[j]=c3[j+1];
c3[j+1]=y;
}
}
}
for(int i=0;i<t;i++)
{
cout<<a1[i]<<" "<<b1[i]<<" "<<c1[i]<<endl;
}
for(int i=0;i<t1;i++)
{
cout<<a2[i]<<" "<<b2[i]<<" "<<c2[i]<<endl;
}
for(int i=0;i<t2;i++)
{
cout<<a3[i]<<" "<<b3[i]<<" "<<c3[i]<<endl;
}
return 0;
}
```
阅读全文