Recently, I wrote less code. I did not have the chance to practice it. I went to some programming websites and made a few questions. Last night I found.
First, find the longest matching string in a long string and its reverse string.
#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: String Reverse Order // parameter: const string & srcstring string to reverse order // returns: STD :: string reverse result //*********************************** * 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 wocould be "racecar ". enter the password to access level 2: * // * fourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainen Too many other othisbutinalargerse Nsewecannotdedicatewecannotconsecratewecannothallowthisgroundthebravelmenlivinganddeadwhostruggledherehaveconsecrateditfarabove ourpoorponwertoaddordetracttgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereitisforust helivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvanceditisratherforustobeherededic atedtothegreattdafs Optional bytes plebythepeopleforthepeopleshallnotperishfromtheearth */# include <iostream> # include <windows. h> # include <ctime> # include <assert. h> # include <string> # Include "moonstring. H "using namespace STD; //************************************// method: getmaxsubstring // access: Public // describe: Find the longest string that overlaps the two strings // 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 Cu Rrlen = 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: String 2 fixed, word Character string 1 is gradually shortened. Find the longest string of the same index // 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 ); /********************************** algorithm start **** * ************************* // const string input_text = "123456789987654321 "; con St string input_text = "too many bytes Zookeeper extends inishedworkwhichthe Too many maid "; stri Ng reversestr = moonstring: reverse (input_text); string maxsubstr1 = reverse (input_text, reversestr); string maxsubstr2 = reverse (reversestr, input_text); string maxsubstr = reverse ()> maxsubstr2.length ()? Maxsubstr1: maxsubstr2; cout <"the eldest son string is:" <Endl <maxsubstr <Endl <"Length:" <maxsubstr. length () <Endl; /*********************************** end of the algorithm **** * *************************/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, extends tTime); cout <Endl <"by godmoon" <Endl <timestr <beep_char; System ("pause");} // main function int main () {F1 (); Return 0;}/* void F1 () is the eldest son of ranynar. The length is 7 Total milliseconds is 182.737by godmoonfri Mar 08 21:41:24 2013 */
The second question is divided into several parts. The first car is to find the minimum prime number greater than 227000 and also the number X of the Fibonacci, and add it to 1 to obtain the sum of all the unique prime factors of Y.
# Pragma once # include <windows. h> class moonmath {public: moonmath (void );~ Moonmath (void ); //************************************// method: isint // access: Public // describe: determines whether the double value is very close to an integer in the Epsilon range. // if the value of 1.00005 is greater than 0.00005, It is very close to an integer. // parameter: double doublevalue: the double value to be determined. // parameter: Double Epsilon: The accuracy of the determination. If 0 <Epsilon <0.5 // parameter: int32 & intvalue is close, returns the nearest integer // returns: bool returns true, otherwise, false is returned //*********************************** * Static bool isint (double doublevalue, double Epsilon, int32 & intvalue ); //************************************// method: sign // access: Public // describe: Get the value symbol // parameter: T value to get the value of the symbol // returns: positive, 0, and negative numbers of int32 return 1, 0, and-1, respectively //************************ * *********** template <typename T> static int32 sign (T value ); //************************************// method: isprimer // access: Public // describe: determines whether a number is a prime number. // parameter: uint32 num indicates the number to be judged. // returns: bool indicates a prime number and returns true, otherwise, false is returned //*********************************** * 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); intva Lue = (FABS (doublevalue-value)> 0.5 )? (Value + moonmath: Sign (doublevalue): (value); Return true;} bool moonmath: isprimer (uint32 num) {// 0 and 1 are not prime if (Num <= 1) {return false;} uint32 sqrtofnum = SQRT (double) num ); // The 2nd power of num // from 2 to SQRT (Num). If no number can be divisible by num, num is the prime number; otherwise, it is not 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 in Structions 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 above. use your code to compute the smallest prime Fibonacci numbergreater than 227,000. call this number X. step 2. the password for Level 3 I S 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: 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: Calculate the sum of the data in the Set container and // parameter: Set <t> contain // returns: t // *********************************** Template <typename T> T calcsum (set <t> contain) {T S Um = 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 ); /********************************** algorithm start **** * *************************/uint32 start_num = 227000; uint32 x = getx (start_num); cout <"greater than" <start_num <"the minimum number of Fibonacci prime numbers x is:" <x <Endl; uint32 y = X + 1; cout <"Y = x + 1 =" <Y <Endl; // obtain all the different prime factor set <uint32> divisors of Y; getprimerdivisors (Y, divisors); // sum uint32 sum = calcsum (divisors); // output cout <"The sum of all prime factors of Y :"; for (set <uint32 >:: iterator it = divisors. begin (); it! = Divisors. end (); It ++) {cout <* It <"+" ;}cout <"\ B =" <sum <Endl; /*********************************** end of the algorithm **** * *************************/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, extends tTime); cout <Endl <"by godmoon" <Endl <timestr <beep_char; System ("pause");} // main function int main () {F1 (); Return 0;}/* void F1 () smaller than 227000 Fibonacci Prime Number X: The sum of all prime factors of 514229y = x + 1 = 514230y: 2 + 3 + 5 + 61 + 281 = 352 total milliseconds is 10.7617by godmoonfri Mar 08 23:23:02 2013 */
The third question is to give a string of numbers to find a group of strings. The maximum number in the string is the sum of other numbers and calculate the number of strings.
/* 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 wocould be1 + 2 = 31 + 3 = 42 + 4 = 61 + 2 + 3 = 6 here is the list of numbers you shoshould run your cod E 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 wocould be 4. enter the password to complete the challenge: returns a string of numbers. The maximum number in the string is the sum of other numbers. The English is really cool. */# 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; // find the maximum number of 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;} // evaluate the array length # define sizeofarray (array) (sizeof (array)/(sizeof (array [0]) void F1 () {cout <"Void F1 ()" <Endl; large_integer timestart, timeend, freq; queryperformancefrequency (& freq); queryperformancecounter (& timestart ); /********************************** algorithm start **** * *************************/const uint32 bits_per_byte = 8; const uint32 max_support_length = sizeof (uin T32) * 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: there are too many array elements to calculate! Only up to "<max_support_length <" elements "<Endl; return;} // sort int32 * pnumarraysorted = new int32 [num]; (void) memcpy (pnumarraysorted, num_array, num * sizeof (* pnumarraysorted); sort (pnumarraysorted, pnumarraysorted + num-1, less <int32> (); // traverses all sub-strings 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 <"Total substrings" <subsetsnum <"<Endl; /*********************************** end of the algorithm **** * *************************/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'; // after calculation, it is called 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") ;}// main function int main () {F1 (); Return 0 ;}/ * void F1 () A total of 179 substrings 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. */