设有n个正整数(n<=20),将它们联成一排,组成一个最大的多位数

来源:互联网 发布:网易邮箱数据 编辑:程序博客网 时间:2024/06/10 03:30

设有n个正整数(n<=20),将它们联成一排,组成一个最大的多位数。
  例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213
  又如:n=4时,4个整数7,13,4,246联接成的最大整数为7424613
程序输入:n
 程序输出:n个数联接成的多位数。

此问题本是老师留的一道作业。我发到csdn上后,没想到讨论还挺激烈。下面摘录一些网友们的代码。http://community.csdn.net/Expert/topic/3372/3372952.xml?temp=.9760858

lujianping(lujianping) 的代码:
#include <stdafx.h>
#include <string.h>

#define min(x,y) ((x)>(y)?(y):(x))

int NewStrcmp(char *pszNum1,char *pszNum2)
{
 if ( strlen(pszNum1) == 0 )
  return -1;
 if ( strlen(pszNum2) == 0 )
  return 1;

 unsigned int nMinLen = min(strlen(pszNum1),strlen(pszNum2));
 int nResult;
 //如果一个串是以另外一个串为前缀的话
 if ( (nResult = strncmp(pszNum1,pszNum2,nMinLen)) == 0 )
 {
  if ( nMinLen == strlen(pszNum1) )
  {
   return NewStrcmp( pszNum1 , pszNum2 + nMinLen );
  }
  else
  {
   return NewStrcmp( pszNum1 + nMinLen , pszNum2);
  }
 }
 else
 {
  //如果不等,则直接返回比较结果
  return nResult;
 }

 return 0;
}
int main(int argc, char* argv[])
{
 // result: 7 4 42 3 1                 s
 //char* pszNum[] = { "7", "42", "3", "4", "1" };
 
 // result: 4 432 42 3 1
 //char* pszNum[] = { "432", "4", "42", "3", "1" };
 
 // result: 71 711 7112 4 432 42 3 1
 char *pszNum[] = { "7771", "7", "77", "7118" , "711" } ;

 //使用冒泡排序
 for ( unsigned int i = 0 ; i <
   sizeof(pszNum)/(sizeof(char *)) ; i ++ )
 {
  for ( unsigned int j = 0 ; j <
   sizeof(pszNum)/(sizeof(char *)) - i - 1; j ++ )
  {
   if ( NewStrcmp(pszNum[j],pszNum[j+1]) < 0 )
   {
    char *pTmp;
    pTmp = pszNum[j];
    pszNum[j] = pszNum [ j + 1] ;
    pszNum[j + 1] = pTmp;
   }
  }
 }

 for ( i = 0 ; i < sizeof(pszNum)/(sizeof(char *)) ; i ++ )
  printf("%s ",pszNum[i] ) ;
 printf("/n") ;
 return 0;
}

whyglinux(山青水秀)的代码1:
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;

struct Comp {
  bool operator()(string s1, string s2) {
    string::size_type pos;
 
    while(1) {
      if (s1.size() > s2.size()) {
        for(pos=0; s1.find(s2, pos) == pos; pos+=s2.size()) ;
        s1.erase(0, pos);
        if(s1.size() == 0 || s1.size() > s2.size()) {
          return s1 > s2;
        }
      }
 
      if(s1.size() < s2.size()) {
        for(pos=0; s2.find(s1, pos)==pos; pos+=s1.size()) ;
        s2.erase(0, pos);
        if(s2.size() == 0 || s1.size() < s2.size() ) {
          return s1 > s2;
        }
      }
 
      if(s1.size() == s2.size()) {
        return s1 > s2;
      }
    }
  }
};
 
int main()
{
  // result: 7 4 42 3 1
  //  char* ps[] = { "7", "42", "3", "4", "1" };
 
  // result: 4 432 42 3 1
  //  char* ps[] = { "432", "4", "42", "3", "1" };
 
  // result: 71 711 7112 4 432 42 3 1
  //  char* ps[] = { "7112", "71", "711", "4", "432", "42", "3", "1" };
 
  // 7 77 7771 7118 711
  //  char* ps[] = { "7771", "7", "77", "7118", "711" };
 
  // 717 7171712 71
  //  char* ps[] = { "71", "717", "717712"};
 
  //7118 7117 711
  char* ps[] = { "7118", "711", "7117" };
 
  vector<string> v(ps, ps+sizeof(ps)/sizeof(*ps)); 
 
  sort(v.begin(), v.end(), Comp()); 
  copy(v.begin(), v.end(), ostream_iterator<string>(cout, " ")); 
  cout << endl; 

whyglinux(山青水秀) 的代码2:
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <cstring>
using namespace std;

bool Compare(const string& s1, const string& s2)
{
  if (s1.size() == 0) return false;
  if (s2.size() == 0) return true;
 
  int min_siz = s1.size()<s2.size() ? s1.size() : s2.size();
 
  if (strncmp(s1.c_str(), s2.c_str(), min_siz) == 0) {
    if (min_siz == s1.size()) {
      return Compare(s1, s2.substr(min_siz));
    }
    else {
      return Compare(s1.substr(min_siz), s2);
    }
  }
  else {
    return s1 > s2;
  }
}
 
int main()
{
  // result: 7 4 42 3 1
  //  char* ps[] = { "7", "42", "3", "4", "1" };
 
  // result: 4 432 42 3 1
  // char* ps[] = { "432", "4", "42", "3", "1" };
 
  // result: 71 711 7112 4 432 42 3 1
  //  char* ps[] = { "7112", "71", "711", "4", "432", "42", "3", "1" };
 
  // 7 77 7771 7118 711 or 77 7 7771 7118 711
   char* ps[] = { "7771", "7", "77", "7118", "711" };
 
  // 717 7171712 71
  //  char* ps[] = { "71", "717", "717712"};
 
  // 7118 7117 711
  //  char* ps[] = { "7118", "711", "7117" };
 
  vector<string> v(ps, ps+sizeof(ps)/sizeof(*ps));
 
  sort(v.begin(), v.end(), Compare);
  copy(v.begin(), v.end(), ostream_iterator<string>(cout, " "));
  cout << endl;
}

Squall1009(钰枫)(学习ing)的代码:
include <iostream>
#include <cmath>
using namespace std;
int weishu(int m);
int fun1(int m,int n);
int main()
{
    const int N=20;
    int b,n,i,j,big;
     int a[N]={0};
    cout<<"请输入您要输入的正整数的个数: ";
    cin>>n;
    for(i=0;i<n;++i)
     {
         cout<<"输入第"<<i+1<<"个数:";
         cin>>a[i];
     }
     for(i=0;i<n;++i)
     {   b=i;
         big=a[i];
         for(j=i+1;j<n;++j)
         if(fun1(a[j],big))
         {
             big=a[j];
          b=j;
      }
    a[b]=a[i];
   cout<<big<<" ";
   }
  cout<<endl;
system("pause");
return 0;

int weishu(int m)
{
    int i=1;
    while(m=m/10)
       ++i;
    return i;
}   

int fun1(int m,int n)
{
    int a,b;
    a=weishu(m);
    b=weishu(n);
    if((m*pow(10.0,b)+n)>(n*pow(10.0,a)+m))
     return 1;
     else
     return 0;
 }

cpunion( int argc, char** argv )的代码:
#define MIN(x,y) (x<y?x:y)

bool Compare(const string& s1, const string& s2)
{
    int ret = strncmp(s1.c_str(), s2.c_str(), MIN(s1.length(), s2.length()));
    if (ret == 0)
        return s1+s2>s2+s1;
    else
        return ret>0;
}

int main()
{
    // 测试数据
    char* ps[] = { "35335","35335353","53553","53553535"};
    // 正确结果 53553535 53553 35335 35335353
   
    vector<string> v(ps,ps+sizeof(ps)/sizeof(*ps));
   
    sort(v.begin(), v.end(), Compare);
    copy(v.begin(), v.end(), ostream_iterator<string>(cout, " "));
    cout << endl;
    system("PAUSE");
    return 0;
}

iicup(双杯献酒)的代码:
// 现在提出一个高效的算法
// 只是看起来冗长一些
// VC 6.0
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 如果s1 应该在s2之前,返回true,否则返回false
bool Compare(const char* s1, const char* s2)
{
 if (s1 == 0) return false;
 if (s2 == 0) return true;
 const char* temps1 = s1;
 const char* temps2 = s2;
 while(!((*temps1 == 0) && (*temps2 == 0)))
 {
  // 字符串相加的效果
  if(*temps1 == 0)
  {
   temps1 = s2;
  }
  if(*temps2 == 0)
  {
   temps2 = s1;
  }
  // 比较
  if(*temps1 != *temps2)
  {
   return *temps1 > *temps2;
  }
  else
  {
   temps1++;
   temps2++;
  }

 }
 // 两个相等
 return true;
}

int main()
{

 // 测试数据
 char* ps[] = { "35335","35335353","53553","53553535"};
 // 正确结果 53553535 53553 35335 35335353

 vector<char*> v(ps,ps+sizeof(ps)/sizeof(*ps));
 
 sort(v.begin(), v.end(), Compare); 
 copy(v.begin(), v.end(), ostream_iterator<char*>(cout, " "));
 cout << endl;
  return 0;
}
// 输出:
// 53553535 53553 35335 35335353

// 现在提出一个基于数字的算法
// VC 6.0
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 如果d1 应该在d2之前,返回true,否则返回false
bool Compare(unsigned int d1, unsigned int d2)
{
 if (d1 == 0) return false;
 if (d2 == 0) return true;
 unsigned __int64 tempd1_1 = d1;
 unsigned __int64 tempd1_2 = d1;
 unsigned __int64 tempd2_1 = d2;
 unsigned __int64 tempd2_2 = d2;
 while(tempd2_2)
 {
  tempd1_1 *= 10;
  tempd2_2 /= 10;
 }
 while(tempd1_2)
 {
  tempd2_1 *= 10;
  tempd1_2 /= 10;
 }

 return tempd1_1+d2 > tempd2_1+d1;
}

int main()
{

 // 测试数据
 unsigned int data[] = { 35335,35335353,53553,53553535};
 // 正确结果 53553535 53553 35335 35335353

 vector<unsigned int> v(data,data+sizeof(data)/sizeof(data[0]));
 
 sort(v.begin(), v.end(), Compare); 
 copy(v.begin(), v.end(), ostream_iterator<unsigned int>(cout, " "));
 cout << endl; 
  return 0;
}
// 输出:
// 53553535 53553 35335 35335353

原创粉丝点击