HDU 1872

来源:互联网 发布:网络语言6的意思 编辑:程序博客网 时间:2024/06/03 19:14

这一题是判断未知序列是否是稳定的,那我们为什么不用一个稳定的排序方法进行排序再跟它一个一个的比较呢。相同就是稳定的,不同就不稳定,或者直接错误。

这种类型的题目用结构体很方便。


还有就是我在判断稳定,不稳定,错误这三个上花费了一点时间,所以说细节代码还是不够扎实。加油;


#include<iostream>using namespace std;struct nood{int g;string s;};void BubbleSort(struct nood a[],int n){for(int i=0;i<n-1;i++)  for(int j=0;j<n-i-1;j++)    if(a[j].g<a[j+1].g)    {    nood temp=a[j];    a[j]=a[j+1];    a[j+1]=temp;}}int IsRight(struct nood a[],struct nood b[],int n){int flag1=0,flag2=0;for(int i=0;i<n;i++){if(a[i].g!=b[i].g){flag1=1;}else if(a[i].s!=b[i].s){flag2=1;}}if(flag1){cout<<"Error"<<endl;for(int i=0;i<n;i++)  cout<<a[i].s<<" "<<a[i].g<<endl;}  else if(flag2&&!flag1) { cout<<"Not Stable"<<endl; for(int i=0;i<n;i++)   cout<<a[i].s<<" "<<a[i].g<<endl; }else if(!flag1&&!flag2){    cout<<"Right"<<endl;}   }int main(){int n;while(cin>>n){nood c1[305],c2[305];for(int i=0;i<n;i++)  cin>>c1[i].s>>c1[i].g;for(int i=0;i<n;i++)  cin>>c2[i].s>>c2[i].g;BubbleSort(c1,n);IsRight(c1,c2,n);    }return 0;}