65,189
社区成员




//求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的
//最长递减子序列为{9,5,4,3,2}
int m_max(int *num,int total)
{
int point1=-1,point2=-1;
int max=1;
int base=1;
bool flags=true;
for(int a=0;a!=total-1;++a)
{
if(num[a]>=num[a+1])
{
if(point1==-1&&flags)
point1=a;
else if(point1!=-1&&flags) point2=a;
++base;
flags=false;
}
else if(num[a]<num[a+1])
{
if(base>max)
{
max=base;
point1=-1;
}
else point2=-1;
flags=true;
base=1;
}
}
for(int a=(point1==-1?point2:point1);a!=(point1==-1?point2:point1)+max;++a)
cout<<num[a]<<" "<<flush;
}
//输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,
void move(int *num,int total)
{
int move1[total],move2[total];
int total1=-1,total2=-1;
for(int a=0;a!=total;++a)
{
if(num[a]%……
[/Quote]
54.调整数组顺序使奇数位于偶数前面。
题目:输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,
所有偶数位于数组的后半部分。要求时间复杂度为O(n)。
//输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于数组的前半部分,
void move(int *num,int total)
{
int move1[total],move2[total];
int total1=-1,total2=-1;
for(int a=0;a!=total;++a)
{
if(num[a]%2)//jishu
{
++total1;
move1[total1]=num[a];
}
else //oushu
{
++total2;
move2[total2]=num[a];
}
}
//yidong memory
memcpy(num,move1,total1+1);
memcpy(num+total1+1,move2,total2);
}
68.把数组排成最小的数。
题目:输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个。
例如输入数组{32, 321},则输出这两个能排成的最小数字32132。
请给出解决问题的算法,并证明该算法。
分析:这是09年6月份百度的一道面试题,
从这道题我们可以看出百度对应聘者在算法方面有很高的要求。
/*
* question description:
* print the number from 1 to max value with N dicemal digits
*
* example: for n = 3;
* the print result is : 1, 2, 3, ..., 999
*
* solution thought:
* as we known, the long long double can only 1.79769e+308 on my machine
* ( cpu i3 330 ), that means the number you can handle is no larger than
* 309 if you use the built-in type.
* so, i think whether we can emulates the decimal scale with a char string
* that only includes: '0', '1', '2', ..., '9'.
*
* the following is the code
*/
#include <iostream>
#include <cstring>
#include <climits>
using namespace std;
/*
* define a class that emulates decimal scale with char*
*/
class CCharDecimal{
public:
CCharDecimal( int numDigits, const char* charDecimal=NULL )
{
m_numDigit = numDigits+1;
m_charDecimal = new char[m_numDigit];
memset( m_charDecimal, '\0', m_numDigit-1 );
if( charDecimal != NULL )
{
strncpy( m_charDecimal, charDecimal, m_numDigit );
m_charDecimal[m_numDigit] = '\0';
}
}
CCharDecimal( const CCharDecimal &value )
{
m_numDigit = value.m_numDigit;
strcpy(m_charDecimal, value.m_charDecimal);
}
~CCharDecimal()
{
m_numDigit = 0;
delete []m_charDecimal;
m_charDecimal = NULL;
}
//the max value of n digits
CCharDecimal MaxValue( void )
{
int i;
char *temp = new char[m_numDigit];
for( i=0; i<m_numDigit-1; i++ )
{
temp[i] = '9';
}
temp[i] = '\0';
CCharDecimal result( m_numDigit, temp );
delete []temp;
temp = NULL;
return result;
}
//prefix
CCharDecimal& operator ++ ()
{
//if the lowest digit now is '9', then we set it to
//'0', and add the next higher with 1
int i = 0;
while( m_charDecimal[i] == '9' )
{
m_charDecimal[i] = '0';
i++;
}
//add the current highest digit with one
//if m_charDecimal is used at the first time, its value
//is 0, so we firstly set it to '0', then start adding
if( m_charDecimal[i]<'0')
{
m_charDecimal[i] = '0';
}
m_charDecimal[i]++;
return *this;
}
//postfix
CCharDecimal operator ++ ( int )
{
CCharDecimal result( m_numDigit, m_charDecimal );
++(*this);
return result;
}
friend bool operator< ( const CCharDecimal & lvalue, const CCharDecimal &rvalue );
friend ostream& operator<< ( ostream& out, const CCharDecimal &value );
private:
int m_numDigit;
char *m_charDecimal;
};
bool operator< ( const CCharDecimal &lvalue, const CCharDecimal &rvalue )
{
if( strcmp( lvalue.m_charDecimal, rvalue.m_charDecimal ) < 0 )
{
return true;
}
return false;
}
ostream& operator<< ( ostream& out, const CCharDecimal &value )
{
int i=0;
while( value.m_charDecimal[i] != '\0')
{
i++;
}
while( i > 0 )
cout << value.m_charDecimal[--i];
return out;
}
int main( void )
{
int n;
cout<< "please input a number: " << endl;
cin>>n;
CCharDecimal value( n , "1");
CCharDecimal maxValue = value.MaxValue();
unsigned int i = 1;
while( value < maxValue )
{
cout << value++ << ' ';
if( i%10 == 0 )
{
cout<< endl;
}
if( i == UINT_MAX )
{
i=1;
}
else
{
i++;
}
}
cout<< value << endl;
return 0;
}
//输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的
#include<iostream>
#include<string>
#include<map>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
int min(int,int);
void m_sort(int num,int m);
bool judge(string &left,string &right,int num);
vector<string> total;
int ma;
int main()
{
int number;
ma=0;
while(cin>>number)
{
char transe[10];
itoa(number,transe,10);
string str(transe);
total.push_back(str);
if(str.size()>ma)
ma=str.size();
m_sort(0,ma);
for(int a=0;a!=total.size();++a)
cout<<total[a]<<endl;
}
return 0;
}
void m_sort(int num,int m)
{
if(num>m)
return;
for(int a=0;a!=total.size()-1;++a)
{
for(int b=a;b!=total.size()-1;++b)
{
if(judge(total[b],total[b+1],num))
{
string str(total[b]);
total[b+1]=total[b];
total[b]=str;
}
}
}
m_sort(++num,m);
}
bool judge(string &left,string &right,int num)
{
if(num==0)
return left[num]>right[num];
//如果之前不等,不再一个优先级里
for(int a=0;a!=min(min(left.size(),right.size()),num);++a)
if(left[a]!=right[a])
return false;
if(left.size()<num&&right.size()>=num)
{
if(right[num]<left[0])
return true;
else if(right[num]==left[0])
for(int a=0;a!=num-1;++a)
{
if(right[a]<right[a+1])
return true;
}
}
else if(left.size()>num&&right.size()<num)
{
if(left[num]>left[0])
return true;
else if(left[num]==left[0])
for(int a=0;a!=num-1;++a)
{
if(right[a]>right[a+1])
return true;
}
}
else if(left.size()>num&&right.size()>num)
return left[num]>right[num];
return false;
}
int min(int a ,int b)
{
return a>b?b:a;
}