Welcome to the dulle Alliance (1). Welcome to the dulle Alliance
These are self-developed over time. In short, we need to believe that the foundation of good algorithms is based on Accumulation and reflection. Some of these are from others' blogs and some are interview questions, baidu's
1. Five monkeys split peaches. In the middle of the night, the first monkey got up first, and it divided the peach into five equal heaps, one more. So it eats one and takes a pile of peaches. The second monkey gets up and looks at it, and there are only four peaches. So we combined the four heaps and divided them into five equal heaps, with one more. As a result, it also eats one and takes away a bunch of monkeys. Q: How many peaches are there at least? (My friend said that this is a primary Olympics question ).
For more information, see add four peaches to the pile. At this time, there are X peaches, and a peach is left:
After the first monkey is divided, there are still (1-1/5) X = (4/5) X;
The second monkey is left after the minute: (1-1/5) 2X;
After dividing the third monkey, there will be: (1-1/5) 3X;
After the fourth monkey is divided, there is still :( 1-1/5) 4X;
The fifth monkey is left after the minute: (1-1/5) 5X = (1024/3125) X;
A = (1024/3125) X;
To make a an integer, the minimum value of X is 3125.
Minus four peaches, so there are at least 3121 peaches.
2. There are 10 people from A to J who work hand in hand to form A ring, where A and B are not adjacent. How many arrangement methods are there?
Solution: Basic ring arrangement algorithm. Formula: N! /N (N is the person who participates in the Arrangement)
For this question, the first ten personal rings are arranged in 10! /10, and then use AB as a person to arrange the ring 9! * 2! /9
If AB is excluded, the answer is 10! /10-9! * 2! /9
3.
For a given string, you can insert a character to convert it into a return text, and calculate the minimum number of characters to be inserted. For example, AB inserts at least one character into bab, aa inserts at least 0 characters, abcd inserts at least three characters into dcbabcd.
Analysis: head and tail pointer Method -- set the two pointers pBegin and pEnd to point to the beginning and end of the string respectively, and compare the value pointed to by the first and end pointers. This may happen:
1) * pBegin = * pEnd, both of them move to the center, that is, pBegin ++; pEnd --;
2) * pBegin! = * PEnd, there are two ways to deal with it: 1. Insert a character equal to * pBegin after pEnd, and then compare it with * pBegin ++ to count the number of remaining characters inserted; 2. Insert characters equal to * pEnd in pBegin, and then pEnd -- continue the comparison to count the number of remaining characters inserted. The minimum number of characters to insert is the minimum of one and two.
Repeat the above process until pBegin and pEnd meet each other.
<Span style = "font-size: 18px;" >#include <stdio. h> # include <string. h> int minChange (char * str, char * pBegin, char * pEnd) {if (str = NULL | pBegin = NULL | pEnd = NULL) {return-1;} else if (* str = '\ 0' | pBegin> = pEnd) {return 0 ;} else {// * pBegin = * pEnd, both of them move to the center, that is, pBegin ++; pEnd --; while (pBegin <pEnd & * pBegin = pEnd) {pBegin ++; pEnd --;} // * pBegin! = * PEnd, determine whether to insert before pBegin or after pEND if (pBegin <pEnd) {int min1 = minChange (str, pBegin, pEnd-1 ); int min2 = minChange (str, pBegin + 1, pEnd); return min1 <min2? Min1 + 1: min2 + 1;} else {return 0 ;}} int main (int argc, char const * argv []) {int min, n; char * pBegin = NULL, * pEnd = NULL; char str [100]; gets (str); n = strlen (str); pBegin = str; pEnd = & str [n-1]; min = minChange (str, pBegin, pEnd); printf ("add at least % d characters \ n", min); return 0 ;} </span>
4. Question: enter a positive number n, and output all sequences that are n continuous positive numbers.
For example, input 15, because 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8 = 15, therefore, three consecutive sequences 1-5, 4-6, and 7-8 are output.
For this question, we can use two variables to represent the subscript and then move. One represents the beginning of the sequence and the other represents the end of the sequence. Stop moving when the first half is greater than n
<Span style = "font-size: 18px;" >#include <iostream> using namespace std; void print_sq (int start, int end) {// print the sequence for (int I = start; I <= end; I ++) cout <I <""; cout <endl ;} void find_the_sq (int num) {// search. If sum is large, start shifts right. If end is small, int start = 1, end = 2, mid = num/2; int sum = start + end; while (start <= mid) {if (sum <num) {sum + = ++ end;} else if (sum> num) {sum-= start ++ ;} else {print_sq (start, end); sum-= start ++ ;}}int main () {find_the_sq (25); return 0 ;}</span>
5. Question: enter two strings to remove all the characters from the first string. For example, if you enter "They are students." and "aeiou", the first string after deletion becomes "Thy r stdnts .".
<Span style = "font-size: 18px;" >#include <stdio. h> # include <string. h> # include <memory. h> void delete_ch (char * src, char * del_chs) {int hash [256]; int I; char * pstrat, * pend; int del_len = strlen (del_chs ); pstrat = src; pend = src; memset (hash, 0, sizeof (int) * 256); // initialize for (I = 0; I <del_len; I ++) {hash [del_chs [I] = 1;} while (* pend) {while (hash [* pend] = 1) // find a character that does not need to be deleted * pend ++; * pstrat ++ = * pend ++; // move both pstart and pend} * pstrat = '\ 0 ';} int main (int argc, char const * argv []) {char src [] = "Theeeey are students"; char del_chs [] = "Taeiou"; delete_ch (src, del_chs); printf ("% s \ n", src); return 0; </span>}