最近寫代碼比較少,苦於沒機會練手,就去找了一些編程網站,做幾道題,昨晚發現http://challenge.greplin.com/有3道題,時間要求20分鐘到3個小時,題目不難,把它完成了,貼出代碼。
第一題,找出一長串字串和其逆序串中,最長相符字串。
#include "MoonString.h"MoonString::MoonString(void){}MoonString::~MoonString(void){}string MoonString::Reverse(const string &srcString){ size_t len = srcString.length(); string outString; for(size_t i = 0; i < len; i++) { outString += srcString[len - i - 1]; } return outString;}
#pragma once#include <string>using namespace std;class MoonString{public: MoonString(void); ~MoonString(void); //************************************ // Method: Reverse // Access: public static // Describe: 字串逆序 // Parameter: const string & srcString 要逆序的字串 // Returns: std::string 逆序結果 //************************************ static string Reverse(const string &srcString);};
/*The Cue Programming ChallengeLevel 1----------------------------------------Embedded in this block of text is the password for level 2.The password is the longest substring that is the same in reverse.As an example, if the input was "I like racecars that go fast"the password would be "racecar".Enter the password to access level 2:*//*FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth*/#include <iostream>#include <windows.h>#include <ctime>#include <assert.h>#include <string>#include "MoonString.h"using namespace std;//************************************// Method: GetMaxSubString// Access: public// Describe: 尋找2個字串中重疊的最長字串// Parameter: const string & str1// Parameter: const string & str2// Returns: std::string//************************************string GetMaxSubString(const string &str1, const string &str2){ size_t len = min(str1.length(), str2.length()); size_t currLen = 0; size_t maxLen = 0; size_t maxSubStrStartIndex = 0; for(size_t i = 0 ; i < len; i++) { if(str1[i] == str2[i]) { currLen++; if(currLen > maxLen) { maxLen = currLen; maxSubStrStartIndex = i - currLen + 1; } } else { currLen = 0; } } return str1.substr(maxSubStrStartIndex, maxLen);}//************************************// Method: GetMaxReverseSubString// Access: public // Describe: 字串2固定,字串1逐漸縮短,找到相同索引最長的字串// Parameter: const string & str1// Parameter: const string & str2// Returns: std::string//************************************string GetMaxReverseSubString(const string &str1, const string &str2){ size_t len = str1.length(); size_t maxLen = 0; size_t currLen = 0; string currSubStr; string maxSubStr; for(size_t i = 0; i < len; i++) { currSubStr = GetMaxSubString(str1.substr(i), str2); currLen = currSubStr.length(); if(currLen > maxLen) { maxLen = currLen; maxSubStr = currSubStr; } } return maxSubStr;}void F1(){ cout << "void F1()" << endl; LARGE_INTEGER timeStart, timeEnd, freq; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&timeStart); /*********************************演算法開始*******************************/ //const string INPUT_TEXT = "123456789987654321"; const string INPUT_TEXT = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"; string reverseStr = MoonString::Reverse(INPUT_TEXT); string maxSubStr1 = GetMaxReverseSubString(INPUT_TEXT, reverseStr); string maxSubStr2 = GetMaxReverseSubString(reverseStr, INPUT_TEXT); string maxSubStr = maxSubStr1.length() > maxSubStr2.length() ? maxSubStr1 : maxSubStr2; cout << "最長子串為:" << endl << maxSubStr << endl << "長度為:" << maxSubStr.length() << endl; /*********************************演算法結束*******************************/ QueryPerformanceCounter(&timeEnd); cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl; time_t currentTime = time(NULL); const char BEEP_CHAR = '\007'; const int MAX_TIME_CHAR = 30; char timeStr[MAX_TIME_CHAR]; ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime); cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR; system("pause");}//主函數int main(){ F1(); return 0;}/*void F1()最長子串為:ranynar長度為:7Total Milliseconds is 182.737By GodMoonFri Mar 08 21:41:24 2013*/
第二題,分為幾部分,第一部車是找出大於227000的最小素數同時也是Fibonacci數X,將其加1得Y,求Y的所有唯一質因數之和。
#pragma once#include <windows.h>class MoonMath{public: MoonMath(void); ~MoonMath(void); //************************************ // Method: IsInt // Access: public // Describe: 判斷double值在epsilon的範圍內是否很接近整數 // 如1.00005在epsilon為0.00005以上就很接近整數 // Parameter: double doubleValue 要判斷的double值 // Parameter: double epsilon 判斷的精度,0 < epsilon < 0.5 // Parameter: INT32 & intValue 如果接近,返回最接近的整數值 // Returns: bool 接近返回true,否則返回false //************************************ static bool IsInt(double doubleValue,double epsilon,INT32 &intValue); //************************************ // Method: Sign // Access: public // Describe: 擷取value的符號 // Parameter: T value 要擷取符號的值 // Returns: INT32 正數、0和負數分別返回1、0和-1 //************************************ template <typename T> static INT32 Sign(T value); //************************************ // Method: IsPrimer // Access: public // Describe: 判斷一個數是否是素數 // Parameter: UINT32 num 要判斷的數 // Returns: bool 是素數返回true,否則返回false //************************************ static bool IsPrimer(UINT32 num);};
#include "MoonMath.h"#include <cmath>MoonMath::MoonMath(void){}MoonMath::~MoonMath(void){}template <typename T>INT32 MoonMath::Sign(T value){ if (value > 0) { return 1; } else if (value == 0) { return 0; } else { return -1; }}bool MoonMath::IsInt(double doubleValue, double epsilon, INT32 &intValue){ if (epsilon > 0.5 || epsilon < 0) { return false; } if(INT32(doubleValue + epsilon) == INT32(doubleValue - epsilon)) { return false; } INT32 value = INT32(doubleValue); intValue = (fabs(doubleValue - value) > 0.5) ? (value + MoonMath::Sign(doubleValue)) : (value) ; return true;}bool MoonMath::IsPrimer(UINT32 num){ // 0和1不是素數 if(num <= 1) { return false; } UINT32 sqrtOfNum = sqrt((double)num); // num的2次方 // 從2到sqrt(num),如果任何數都不能被num整除,num是素數,否則不是 for(UINT32 i = 2; i <= sqrtOfNum; ++i) { if(num % i == 0) { return false; } } return true;}
/*The Cue Programming ChallengeLevel 2----------------------------------------Congratulations. You have reached level 2.To get the password for level 3, write code to find the first primefibonacci number larger than a given minimum. For example, the firstprime fibonacci number larger than 10 is 13.When you are ready, go here or call this automatednumber (415) 799-9454.You will receive additional instructions at that time. For the second portionof this task, note that for the number 12 we consider the sum of the prime divisorsto be 2 + 3 = 5. We do not include 2 twice even though it divides 12 twice.Enter the password to access level 3:*//*上面的go here:Step 1. Use your code to compute the smallest prime fibonacci numbergreater than 227,000. Call this number X.Step 2. The password for level 3 is the sum of prime divisors of X + 1.Note: If you call the number instead, it will check your answer for step 1.*/#include <iostream>#include <windows.h>#include <ctime>#include <set>#include "MoonMath.h"using namespace std;//************************************// Method: GetNextFibonacciNum// Access: public// Describe: 擷取Fibonacci數列,每次擷取都是擷取到下一個,因此是一次性函數// Returns: UINT32//************************************UINT32 GetNextFibonacciNum(){ static UINT32 lastNum = 0; static UINT32 currNum = 1; currNum += lastNum; lastNum = currNum - lastNum; return lastNum;}UINT32 GetX(UINT32 startNum){ UINT32 X; while((X = GetNextFibonacciNum()) <= startNum); while(!MoonMath::IsPrimer(X)) { X = GetNextFibonacciNum(); } return X;}void GetPrimerDivisors(UINT32 num, set<UINT32> &divisors){ UINT32 sqrtOfY = sqrt((double)num); for(UINT32 i = 2; i <= sqrtOfY; ++i) { if(!MoonMath::IsPrimer(i)) { continue; } if(num % i == 0) { divisors.insert(i); num /= i; sqrtOfY = sqrt((double)num); } } if(MoonMath::IsPrimer(num)) { divisors.insert(num); }}//************************************// Method: CalcSum// Access: public// Describe: 計算set容器內資料的和// Parameter: set<T> contain// Returns: T//************************************template <typename T>T CalcSum(set<T> contain){ T sum = 0; for(set<T>::iterator it = contain.begin(); it != contain.end(); ++it) { sum += *it; } return sum;}void F1(){ cout << "void F1()" << endl; LARGE_INTEGER timeStart, timeEnd, freq; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&timeStart); /*********************************演算法開始*******************************/ UINT32 START_NUM = 227000; UINT32 X = GetX(START_NUM); cout << "大於" << START_NUM << "的最小的Fibonacci質數X是:" << X << endl; UINT32 Y = X + 1; cout << "Y = X + 1 = " << Y << endl; // 獲得Y的所有不同的質因數 set<UINT32> divisors; GetPrimerDivisors(Y, divisors); // 求和 UINT32 sum = CalcSum(divisors); // 輸出 cout << "Y的所有質因數之和為:"; for(set<UINT32>::iterator it = divisors.begin(); it != divisors.end(); it++) { cout << *it << " + "; } cout << "\b\b= " << sum << endl; /*********************************演算法結束*******************************/ QueryPerformanceCounter(&timeEnd); cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl; time_t currentTime = time(NULL); const char BEEP_CHAR = '\007'; const int MAX_TIME_CHAR = 30; char timeStr[MAX_TIME_CHAR]; ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime); cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR; system("pause");}//主函數int main(){ F1(); return 0;}/*void F1()大於227000的最小的Fibonacci質數X是:514229Y = X + 1 = 514230Y的所有質因數之和為:2 + 3 + 5 + 61 + 281 = 352Total Milliseconds is 10.7617By GodMoonFri Mar 08 23:23:02 2013*/
第三題,給一串數字,從中找出一組字串,字串中最大的數字是其他數字之和,求字串的數目。
/*The Cue Programming ChallengeLevel 3----------------------------------------Congratulations. You have reached the final level.For the final task, you must find all subsets of an arraywhere the largest number is the sum of the remaining numbers.For example, for an input of:(1, 2, 3, 4, 6)the subsets would be1 + 2 = 31 + 3 = 42 + 4 = 61 + 2 + 3 = 6Here is the list of numbers you should run your code on: 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99The password is the number of subsets. In the above case theanswer would be 4.Enter the password to complete the challenge:給一串數字,從中找出一組字串,字串中最大的數字是其他數字之和,求字串的數目。英文真囉嗦。*/#include <iostream>#include <windows.h>#include <ctime>#include <algorithm>#include <functional>using namespace std;template <typename T>bool CheckSubset(UINT32 bitMap, const T array[], UINT32 arraySize){ if(bitMap == 0) { return false; } UINT32 index = arraySize - 1; T maxNum = 0; T currSum = 0; // 尋找最大數 while((bitMap & 1) != 1) { --index; bitMap >>= 1; } maxNum = array[index]; do { bitMap >>= 1; --index; if((bitMap & 1) == 1) { currSum += array[index]; } } while(bitMap != 0); return currSum == maxNum;}// 求數組長度#define SizeofArray(array) ((sizeof(array))/(sizeof(array[0])))void F1(){ cout << "void F1()" << endl; LARGE_INTEGER timeStart, timeEnd, freq; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&timeStart); /*********************************演算法開始*******************************/ const UINT32 BITS_PER_BYTE = 8; const UINT32 MAX_SUPPORT_LENGTH = sizeof(UINT32) * BITS_PER_BYTE; const INT32 NUM_ARRAY[] = {3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99}; UINT32 num = SizeofArray(NUM_ARRAY); if(num > MAX_SUPPORT_LENGTH) { cout << "Error: 數組元素太多,無法計算!僅支援最多" << MAX_SUPPORT_LENGTH << "個元素" << endl; return; } // 排序 INT32 *pNumArraySorted = new INT32[num]; (void)memcpy(pNumArraySorted, NUM_ARRAY, num * sizeof(*pNumArraySorted)); sort(pNumArraySorted, pNumArraySorted + num - 1, less<INT32>()); // 遍曆所有子串 UINT32 bitMap; UINT32 maxBitMap = 1 << (num + 1); UINT32 subsetsNum = 0; for(bitMap = 1; bitMap < maxBitMap; ++bitMap) { if(CheckSubset(bitMap, pNumArraySorted, num)) { ++subsetsNum; } } delete[] pNumArraySorted; cout << "共有子串" << subsetsNum << "個" << endl; /*********************************演算法結束*******************************/ QueryPerformanceCounter(&timeEnd); cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl; time_t currentTime = time(NULL); const char BEEP_CHAR = '\007'; // 計算完畢叫一聲 const int MAX_TIME_CHAR = 30; char timeStr[MAX_TIME_CHAR]; ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime); cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR; system("pause");}//主函數int main(){ F1(); return 0;}/*void F1()共有子串179個Total Milliseconds is 1249.17By GodMoonSat Mar 09 08:45:07 2013*//*The Cue Programming ChallengeThe End----------------------------------------Congratulations. You completed the challenge. Your completion token is *******-***-***-***.We'd love to talk to you - send your completion token, the code you wroteduring the challenge, and your resume tojobs+i+solved+the+challenge@cueup.comEven if you're not looking for a job, we'd love to hear what you thoughtabout the challenge.For a new challenge, see if you can complete The Colossal Cue Adventure.*/