uva120

作者: Amosasas | 来源:发表于2017-11-18 13:39 被阅读0次

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> ans;
char str[256];
int a[31];
int sorted[31];
int cnt;

void flap(int p){
    ans.push_back(p+1);
    for(int i=0;i<p/2;i++){
        swap(a[i],a[p-i]);
    }
    printf("debug:  ");
    for(int i=0;i<cnt;i++){
        printf("%d ", a[i]);
    }
    printf("\n");

}


int main ()
{

//  cin.getline(str, 256);
//  char * pch;
//  pch = strstr (str,"simple");
//  strncpy (pch,"sample",6);
//  puts (str);
  while(cin.getline(str, 256)){
      char *loc;
      cnt = 0;
      ans.clear();
      a[cnt++] = atoi(str);
      loc = strstr(str, " ");
      while(loc != NULL){
        a[cnt++] = atoi(loc);
        loc = strstr(loc + 1, " ");
      }
      for(int i=0;i<cnt;i++){
        sorted[i] = a[i];
      }
      sort(sorted,sorted+cnt);
      printf("debug sort:  ");
      for(int i=0;i<cnt;i++){
         printf("%d ", sorted[i]);
      }
      printf("\n");
      for(int j=cnt-1;j>=0;j--){
        if(a[j]!=sorted[j]){
            printf("!equ %d\n", a[j]);
            int pos;
            for(pos = 0; pos <= j; pos++){
                if(a[pos]==sorted[j]){
                    break;
                }
            }
            if(pos!=0){
                flap(pos);
            }
            flap(j);
        }
      }
      for(int i=0;i<ans.size();i++){
        printf("%d ", ans[i]);
      }
      printf("\n");


  }

  return 0;
}

相关文章

网友评论

      本文标题:uva120

      本文链接:https://www.haomeiwen.com/subject/jguevxtx.html