PAT 1038. Recover the Smallest Number

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<iomanip>
#include<algorithm>
using namespace std;

struct Node
{
  char strNum[10];
  bool isUsed;
};

//获得字典序最小的数字,321,32这种情况要特殊考虑。
string getMinStr(vector<Node> &v)
{
  string strMin = "";
  int i, index = -1, indexMin = -1;
  for(i=0; i<v.size(); i++)
    if(!v[i].isUsed)
    {
      strMin = v[i].strNum;
      indexMin = i;
      v[i].isUsed = true;
      break;
    }
  
  for(i=0; i<v.size(); i++)
    if(!v[i].isUsed && (v[i].strNum+strMin) < (strMin+v[i].strNum) )
    {
      strMin = v[i].strNum;
      index = i;
    }
  if(index != -1 && index!=indexMin)
  {
    v[index].isUsed = true;
    v[indexMin].isUsed = false;
  }
  return strMin;
}

int main()
{
  Node node;
  vector<Node> vNode;
  int N,i;
//  cin>>N;
  scanf("%d",&N);
  for(i=0; i<N; i++)
  {
    scanf("%s",node.strNum);
//    cin>>node.strNum;
    node.isUsed = false;
    vNode.push_back(node);
  }
  string strNum = "";
  string strTmp = getMinStr(vNode);
  while( strTmp != "")//若相等,则表示所有的数据已经用完。
  {
    strNum += strTmp;
    strTmp = getMinStr(vNode);
  }

  for(i=0; i<strNum.length(); i++)
    if(strNum[i] != '0')
      break;

  //全部都为0的情况
  if(i == strNum.length())
    cout<<0<<endl;
  else
  {
    for(;i<strNum.length(); i++)
      cout<<strNum[i];
    cout<<endl;
  }

  return 0;
}

  

posted @ 2012-12-15 15:19  Frank@609  Views(297)  Comments(0Edit  收藏  举报