还是比较基础的面试题:
</pre><pre name="code" class="cpp">#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <vector>
#include <list>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
typedef basic_string<char>::size_type S_T;
bool Comp(const string &a,const string &b)
{
if(a.size() > b.size())
{
return false;
}
else if(a.size() < b.size())
{
return true;
}
char * p_a;
char * p_b;
p_a =(char *)a.c_str();
p_b =(char *)b.c_str();
while(*p_a!='\0')
{
if(*p_a == *p_b)
{
p_a++;
p_b++;
continue;
}
else if(*p_a < *p_b)
{
return true;
}
else
{
return false;
}
}
if(*p_a == '\0')
{
return true; //a小 交换
}
else
{
return false;
}
}
void QuZero(string &str_In)
{
string tmp = str_In;
int strLen = (int)tmp.size();
char * p = (char *)tmp.c_str();
int iStartIndex = 0;
while(*p=='0')
{
str_In = str_In.substr(iStartIndex,strLen);
p++;
iStartIndex++;
}
if(*p='\0')
{
str_In = "0";
}
}
//主函数是个测试用例
int main()
{
char ch_1[10000];
char ch_2[10000];
char ch_3[10000];
char ch_4[10000];
char ch_5[10000];
memset(ch_1,0,10000);
memset(ch_2,0,10000);
memset(ch_3,0,10000);
memset(ch_4,0,10000);
memset(ch_5,0,10000);
cin>>ch_1;
cin>>ch_2;
cin>>ch_3;
cin>>ch_4;
cin>>ch_5;
string str_In_1(ch_1);
string str_In_2(ch_2);
string str_In_3(ch_3);
string str_In_4(ch_4);
string str_In_5(ch_5);
string str_max;
string str_min;
QuZero(str_In_1);
QuZero(str_In_2);
QuZero(str_In_3);
QuZero(str_In_4);
QuZero(str_In_5);
std::vector<string> m_vec;
std::vector<string> m_vec_show;
m_vec.push_back(str_In_1);
m_vec.push_back(str_In_2);
m_vec.push_back(str_In_3);
m_vec.push_back(str_In_4);
m_vec.push_back(str_In_5);
m_vec_show = m_vec;
sort(m_vec.begin(),m_vec.end(),Comp);
str_max = m_vec[4];
str_min = m_vec[0];
//交换大的数
int IndexMax;
for(int m=0;m<5;m++)
{
if(str_max.compare(m_vec_show[m]) == 0)
{
IndexMax = m;
}
}
string str_tmp = m_vec_show[4];
m_vec_show[4] = str_max;
m_vec_show[IndexMax] = str_tmp;
//交换最小数
int IndexMin;
for(int mn=0;mn<5;mn++)
{
if(str_min.compare(m_vec_show[mn]) == 0)
{
IndexMin = mn;
}
}
str_tmp = m_vec_show[0];
m_vec_show[0] = str_min;
m_vec_show[IndexMin] = str_tmp;
int iShow = (int)m_vec_show.size();
int ii = 0;
for(ii=0;ii<iShow;ii++)
{
printf("%s ",m_vec_show[ii].c_str());
}
}
很仓促写的答案,bug什么的也有,只不过通过了测试用例。
题目主要考察了超过INT64的数字处理,输入002去整方案,等等。