输入数组元素,由这些元素组成的最小数字

来源:互联网 发布:qq活动抽奖软件 编辑:程序博客网 时间:2024/05/16 02:01


RT,给数输入32,321.。。。两个值,输出由数组中数字组成的最小数值。

源程序如下:


#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<algorithm>


using namespace std;
//按照升序排列
bool compare(const string str1,const string str2)
{
 string s1,s2;
 s1=str1+str2;
 s2=str2+str1;
 return s1<s2;
}
int main()
{
 vector<int>data;
 data.push_back(32);
 data.push_back(321);
 //cout<<"data[0]="<<data[0]<<endl;
//s string f("abc");
 //vector<string> strArray;
 int len=data.size();
 string *p=new string[len];
 int i=0;
 for(;i<len;i++)
 {
  stringstream ss_stream;
 // ss_stream.clear();
 // ss_stream << true;
  ss_stream<<data[i];
  ss_stream>>p[i];
 // cout<<"p[0]="<<p[0]<<endl;
 
 } 
 sort(p,p+len,compare);
 //cout<<"p[0]="<<p[0]<<endl;
 int j=0;
 for(;j<len;j++)
 {
  cout<<p[j];
 //s cout<<11<<endl;
  
 }
 cout<<endl;
 delete[] p;
 p=NULL;
 
 return 0;
 
}

0 0
原创粉丝点击