一个C++面试题的数组和STL解法

来源:互联网 发布:网络教育统考时间安排 编辑:程序博客网 时间:2024/04/29 21:40

在CSDN上看一个题目:是要把输入的各个数字从中间开始一左一右的排列出来。如果输入的数字有奇数个就正中间还开始,如果输入的数字有偶数个,就从中间偏左的一个数字开始。举例:如果输入为1, 2, 4,5,3   输出应该是4,2,1,3,5; 如果输入为1,3,2,5,6,4  输出应该是 5, 3, 1, 2, 4, 6.

 

对于这个题目,可以使用数组,当然如果可以用STL的话,也可以有list来做。

先看一下使用数组的解法:

 

// Try so solve a quiz for fresh graduate test from Huawei 2011#include <iostream>#include <iomanip>using std::cin;using std::endl;using std::cout;template <class T> int getArrayLen( T & array)  //使用模板定义一个函数getArrayLen,来返回数组Array的长度{return (sizeof(array) / sizeof(array[0]));}int QuizFunction( int inputQ[ ], int k);int main(){const int MAX(10000);int input[ MAX ];int n = 0;cout << endl << "Please input an integer less than 1001: ";cin >> n;for (int i = 0; i < n; i++)input[i] = i;cout << endl << "n is: "<< n << endl;int m = getArrayLen(input);cout << "Array size is: " << m << endl;QuizFunction( input, n );return 0;}int QuizFunction( int inputQ[], int k){int temp(0);int outputQ[10000];#pragma region Sort the input arrayfor ( int i = 1; i < k; i++){if (inputQ[i-1] > inputQ[i]){temp = inputQ[i];inputQ[i] = inputQ[i-1];inputQ[i-1]= temp;}}#pragma endregion#pragma region Print the Input arraycout << "The Input is: " << endl;for ( int i = 0; i < k; i++)cout << inputQ[i] << " ";#pragma endregion#pragma region Put the elements in the desired order into the Result Arrayfor (int i = 0; i < k; i++){if ( k % 2 == 0){if ( i % 2 ==0) outputQ[ k/2 -1 - ( i + i % 2) / 2] = inputQ[i];elseoutputQ[ k/2 -1  + ( i + i % 2) / 2] = inputQ[i];}else {if ( i % 2 ==0) outputQ[ (k-1)/2 - ( i  + i % 2) / 2] = inputQ[i];elseoutputQ[ (k-1)/2 + ( i + i % 2) / 2] = inputQ[i];}}#pragma endregion#pragma region Print the output arraycout << endl << "The Output is: " << endl;for (int i = 0; i < k; i++)cout << outputQ[i] << " ";cout << endl;return 0;#pragma endregion}

再来看STL的做法:

 

// Ex10_05_Quiz.cpp// Working with a list to solve a quiz from Huawei for 2012 fresh graduates#include <iostream>#include <list>#include <string>using std::cin;using std::cout;using std::endl;using std::list;using std::string;int main(){list<int> integerListInput;//Put the initial inputs herelist<int> integerListResult;// Put the result herelist <int>::iterator iter;// Stores an iterator in the input / output listint i = 0;#pragma region  Read the data from cmdcout << "Enter a  number less than 10001.  Just press Enter to end:"<< endl;int number =0;cin >> number;#pragma endregion#pragma region insert data 0 to i-1 into the input listfor (int i = 0 ; i < number; i ++){iter = integerListInput.begin();integerListInput.insert(iter,i);}#pragma endregion#pragma region Output the data using an iteratorcout << endl << "Here is the numbers you entered:" << endl;for ( iter = integerListInput.begin(); iter != integerListInput.end(); iter ++ )cout << *iter << " ";#pragma endregion#pragma region Sort the data in ascending ordercout << endl << "In ascending sequence the sentences you entered are:" << endl;integerListInput.sort();for ( iter = integerListInput.begin(); iter != integerListInput.end(); iter++)cout << *iter << " ";#pragma endregion#pragma region insert the sorted data according to odd/even cursor of the itemfor( iter = integerListInput.begin(); iter != integerListInput.end(); iter++){i++;if( i % 2==0) integerListResult.push_front(*iter);elseintegerListResult.push_back(*iter);}#pragma endregion// Clear i valuei = 0;//Output the result Listcout << endl << "The result list is: " << endl;for( iter = integerListResult.begin(); iter != integerListResult.end(); iter++) cout << *iter << " ";return 0;}

 

以上在VS2010 下调试通过。