/* just like bubble sort swapping times */ #include <iostream> #include <algorithm> using namespace std; int main(int argc, char *argv[]){ int t; cin >> t; int train[51]; while(t--){ int length; int count=0; cin >> length; for (int i=0; i<length; ++i){ cin >> train[i]; } for (int i=0; i<length-1; ++i){ for (int j=0; j<length-i-1; ++j){ if (train[j]>train[j+1]) swap(train[j], train[j+1]), count++; } } cout << "Optimal train swapping takes " << count << " swaps." << endl; } return 0; }
沒有留言:
張貼留言