下面是自己做的一些面试题解答,均运行通过,但是时间效率不一定最佳。
为了以后方便自己先写了个计时类和一个打印数组的函数
//使用方法: CLOCK{ //计时代码 }
class Clock
{
public:
Clock()
{
startTime = GetTickCount();
isValid = true;
cout << "-------------记时开始--------------------" << endl;
}
operator bool()
{
return isValid;
}
Clock& operator++()
{
isValid = !isValid;
return *this;
}
Clock & operator++(int)
{
isValid = !isValid;
return *this;
}
void setStartTime(DWORD st)
{
startTime = st;
}
~Clock()
{
cout << "-------------记时结束--------------------"<< endl;
cout << "本次运行时间:" << GetTickCount() - startTime << "ms" << endl;
}
private:
bool isValid;
DWORD startTime;
};
#define CLOCK for(Clock clo; clo; clo++)
void showArr(int * arr, int length, int begin = 0)
{
if(!arr || length < 0 || begin >= length)
return ;
for(int i = begin; i < length; ++i)
cout << arr[i] << "\t";
cout << endl;
}