用插入排序怎么做

P1093 [NOIP2007 普及组] 奖学金

lsx_nsgl @ 2021-10-05 08:16:23

这程序0分


using namespace std;
int a[310],b[310],c[310],num[310],total[310],n;
void zzswap(int a,int b){
    int t;
    t=a;
    a=b;
    b=t;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i]>>c[i];
        num[i]=i;
        total[i]=a[i]+b[i]+c[i];
    }
    for(int i=1;i<=n;i++){
        int minn=0;
        for(int j=i+1;j<=n;j++){
            minn=max(minn,total[j]);
        }
        for(int j=i+1;j<=n;j++){
            if(total[j]==minn){
                cout<<i<<j<<endl; 
                zzswap(total[i],total[j]);
                zzswap(num[i],num[j]);
                cout<<total[i]<<total[j]<<endl; 
            }
        }
    }
    for(int i=1;i<=5;i++){
        cout<<num[i]<<" "<<total[i]<<endl;
    }
    return 0;
}```

by Yuzu_Soft @ 2021-10-05 08:23:59

你这swap有问题,主程序还是没交换的状态


by BreakPlus @ 2021-10-05 08:25:12

果然是 zz swap


by Zelotz @ 2021-10-05 08:50:19


void zzswap(int &a,int &b){
    int t;
    t=a;
    a=b;
    b=t;
}

by cmaths @ 2021-10-05 09:02:18

建议使用algorithm里的swap或者指针


|