您的位置:首页 > 移动开发 > IOS开发

My solution for SRM 306 DIV2 [250]

2006-06-09 09:05 531 查看
Problem Statement: http://www.topcoder.com/stat?c=problem_statement&pm=6413&rd=9986

My solution:

#include <iostream>
#include <vector>

using namespace std;

class SortMachine  {
 
public:
  SortMachine () {}
 
public:
  int countMoves(vector <int> a) {
    int countMove = 0;

    while(1) {
      int move = -1;
      int minInversion = 1001;
     
      for (unsigned int i = 0; i < a.size() - 1; ++i) {
        for (unsigned int j = i + 1; j < a.size(); ++j) {
          if (a[i] > a[j] && a[i] < minInversion) {
            move = i;
            minInversion = a[i];
            break;
          }
        }
      }
     
      if (move == -1) {
        break;
      }
     
      int temp = a[move];     
      for (unsigned int i = move; i < a.size() - 1; ++i) {
        a[i] = a[i + 1];
      }
      a[a.size() - 1] = temp;

      ++countMove;
    }
   
    return countMove;
  }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  div vector iostream class