http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10594&courseid=4

 新生排队

Problem description
  某班上现在有N个新生,按学号排队去操场做操,现在需要按身高M从低到高排队,只能相邻的两个交换,问排好这个队所有同学最少要多交换多少次才能排好?

Input
  有多组测试数据

 

第一行输入学生人数N0),即学号最大数

第二行按学号输入每个学生的身高M (假设单位:nm)0

N=0时不需处理

Output
  输出最少的交换次数即可,每组数据占一行

Sample Input
5
9 1 0 5 4
4
1 2 3 4
0
Sample Output
6
0
wrong solution:
//Runtime Error
#include
using namespace std;
int a[100];
int main()
{
    int m,i,j,num,sum;
    while(scanf("%d",&m)!=EOF)
    {
                              if(m==0)
                              break;
                              for(i=0;i                              scanf("%d",&a[i]);
                              sum=0;
                              while(1){
                              i=0;j=1;
                              num=0;
                              while(j                              if(a[i]>a[j]){
                              swap(a[i],a[j]);
                              //cout<<"i="<                              num++;
                              }
                              i++;j++;
                              }
                              sum+=num;
                              if(num==0)
                              break; 
                              }
                              printf("%d/n",sum);
                              }
                              system("pause");
                              return 0;
                             
    }
// look for help 

你可能感兴趣的:(http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10594&courseid=4)