C++ 157ms

#include <iostream> 


 using namespace std; 


 int bubbleSort(int * array, int length) { 


     int exchangeTime = 0; 

     for (int i = 0; i < length-1; i++) { 

         bool changed = false; 

         for (int j = 0; j <= length - 1 - i -1; j++) { 

             if (array[j] > array[j+1]) { 

                 exchangeTime++; 

                 int tmp = array[j]; 

                 array[j] = array[j+1]; 

                 array[j+1] = tmp; 

                 changed = true; 

             } 

         } 

         if (!changed) { 

             break; 

         } 

     } 

     // cout<<exchangeTime<<endl; 

     return exchangeTime; 

 } 


 int main() { 

     int caseNum = 0; 

     cin>>caseNum; 

     for (int k = 0; k < caseNum; k++) { 

         int num = 0; 

         cin>>num; 

         if(!num) { 

             return 0; 

         } 

         int * array = new int[num]; 

         for(int i = 0; i < num; i++) { 

             cin>>array[i]; 

         } 

         int swapNum = bubbleSort(array, num); 

         cout<<"Scenario #"<<k+1<<":"<<endl; 

         cout<<swapNum<<endl<<endl; 

         delete[] array; 

         array = NULL;         

     } 

     cout<<endl; 
}

解2299的副产品,本质一样, 不过2299对时间更敏感,因此冒泡会TLE(冒泡 O(n2方)).

1804的数据弱, 冒泡还可以.

本着多刷题, 就把2299的copy了一份把1804给解了.

水题,没啥说的, 输出注意留空格就OK不然PE。

权当练习冒泡排序.