#include <iostream> #include <math.h> #include <string.h>using namespace std;// Cow birth: A baby cow can give birth to the first cow in the 4th year after a year//existing a baby cow 20 years Total number of cows int cownum (int size) {int* Year = new int[size];memset (year, 0, size*sizeof (int)); year[0] = 1;int count = 0;for (int i = 0; i < size; ++i) {if (i >= 4-1 ) {year[i] = year[i - 1] + year[i - 3];}} for (int i = 0; i < size; ++i) Count += year[i];return count;delete year;} INT&NBSP;COWNUM1 (int year) {int newcow1 = 1;//1 ox start with 1 int newcow2 &NBSP;=&NBSP;0;//2 Ox int newcow3 = 0;//three years Ox int newcow4 = 0;//four years cattle Adult Cow//int newcow5 = 0;//will test instructions understand as the 5th year the cow begins to live add a 5-year-old cattle number int oldcow = 0;//Total for (int yearcnt = 2; yearcnt ), Cub Cow int count = 0; // <=&NBSP;YEAR;&NBSP;++YEARCNT)//Default ox is 1 years old first time to enter the ox 2 years old {//newcow5 = newcow4;newcow4 = &NBSP;NEWCOW3;&NBSP;//3 Longhorn 4 year old oldcow += newcow4;newcow3 = newcow2;newcow2 = newcow1;//one-year-old ox 2-year-old NEWCOW1&NBSP;=&NBSP;OLDCOW;//1-year-old cow for the number of calves to be born count = oldcow + Total number of newcow1+newcow2+newcow3;//is adult ox + 1 2 3 year old Ox//count = oldcow + newcow1 + newcow2 + newcow3+newcow4; //if it starts at 5 years old//total adult bovine + 1 2 3 4-year-old bull}return count;} A string of beads a m<=10 color n for the smallest substring with all colors//logic complex problem Void allcolorbead (char* bead, int colornum)//bead Bead string colorNum number of colors {char* head,* str=bead; //head shortest sub-string head str cyclic variable Int clrnum = 0 ,beadcnt=0,tmpcnt=1000000,tmpclrnum=-1The char colorarr[128] = { 0 };//is used to determine whether each color first appears to distinguish the number of colors +1while (*str!= ') { if (* (str + 1) != *str) //If the connected color jumps directly over {char* tmp = str; // Record str If the substring meets the requirements head=strwhile ((*tmp)! = ' \ ") {if (colorarr[*tmp] == 0)//color first appears &NBSP;CLRNUM++{++CLRNUM;++COLORARR[*TMP];} if (clrnum == colornum) //tell bead Color number logic//{//if (TMPCNT&NBSP;>&NBSP;BEADCNT)// If the substring meets the requirements head = str jumps out of the loop//{//tmpcnt = beadcnt;//head = str;//}//break ;//}if (Tmpclrnum==clrnum)//Don't tell beads color number First time need to fully traverse to find out the number of colors break;++beadcnt;++tmp;} if (Tmpcnt > beadcnt&&clrnum>=tmpclrnum)//If the substring satisfies the requirements head = str Jump out of the loop {tmpcnt = beadcnt;tmpclrnum = clrnum;//does not tell the bead color number needs to add the condition head = str;} clrnum = 0; //each retrieval substring End counter system 0 Beadcnt = 0;memset (colorarr, 0, 128 *&nbsP;sizeof (char));} str++; //Jump knife Next substring}for (int i = 0; i <= tmpcnt; ++i)//Output substring cout << * (head + i); Cout << endl;} N*n back glyphs serpentine arrays output by rows// eg: n=3// 1 2 3// 8 9 4// 7 6 5void snakearr (int n) {int* arr = new int[n*n]; // Storage array int type = 0;//write array type 0 left-to-right 1 top-down 2 from right to left 3 up int num = 1;//input Array value 1---n*nint gap = 0;//output span per line int i = 0, j = 0;//array subscript while (num <= n*n)//num add to N*n description full fill End Loop {if (type == 0)//left-to-right write array {for (int index = i + Gap; i < n - gap&&num <= n*n; ++i) arr[i+j*n] = num++;++type;//Rampage Write Array completion Type +1 variable vertical lines ++j;//j+1 to the next line}if (type ==1)//from top to bottom {--i;//i back to outermost for (int index = j + gap; &NBSP;J<&NBSP;N&NBSP;-&NBSP;GAP&&NUM&NBSP;<=&NBSP;N*N;&NBSP;++J) arr[i + j*n] = num++;++type;--i; //i back to outermost-1}if (TYPE&NBSP;==&NBSP;2)//right-to-left {--j;//j fallback to not write outermost for (int index = i - gap; i >= gap&&num <= n*n; - -i) arr[i + j*n] = num++;++type;++i;//i to the left without traversing the outermost}if (type == 3)//from the lower lane {--j;// J fallback to the outermost for not written (int index = n - gap; j > gap&&num <=&NBSP;N*N;&NBSP;--J) arr[i + j*n] = num++;++j;//j back to the outermost ++i;//i not written to the left without traversing the outermost type = 0;//Write direction type Reset 1}gap++;//Four directions write complete span +1}for (int index = 0; index < n*n; ++index) cout << arr[index] << ' '; Cout <<&nbSp;endl;delete arr;} In a string delete extra words specifier find the longest palindrome string length//eg:abbedbba result abbebba //logical large Very complex seems to have high-end algorithm Int plalindromenum (char* str,int start,int end) { if (* STR&NBSP;==&NBSP;0)//judgment pointer validity return 0;int count = 0;//the number of characters in the current string int bigcnt = 1;//maximum palindrome string character number int right = end;//critical value while (start<end&&count!=2)// If you find a match for the last current character or string to tail jump out loop {//start and end are the two identical characters of the current substring for (int i = start + 1; i <=right; ++i) {if (Str[i] == str[start]) {End = i;count = 2;//has the same item count as 2}}if (count != 2)//No same item skip to next character + + Start;} if (END&NBSP;-&NBSP;START>2)//recursive Find a pair of other identical characters in the same number Count += plalindromenum (str, start + 1, end - 1);else if (end - start == 2)//When End-start The 2 description contains several separate characters count++{++count;} if (right -end>1)//If the threshold value-end>1 the description followed by the character Traversal {if (Bigcnt < count)// The count will be saved placed 0bigcnt = count;count = 0;count += plalindromenum ( Str, end+1 , right);//start with a substring to find the matching character}if (Bigcnt < count) bigcnt = count;//always keep bigcnt as the number of characters of the longest palindrome string return bigcnt;} Implementation of N-bit gray code with recursion //so far not to understand////char* gray (int n)//{//char *arry = (char*) malloc (sizeof (char) * (int) pow (2, n) +1);//if (n =1)//{//arry[0] = ' 0 ';//arry[1] = ' 1 ';//arry[2] =0 ;//return arry ;//}////char * tmp=gray (n - 1); /int j = (int) pow (2, n) - 2;//for (int i = 0;i<j; ++ i)//{//arry[i] = tmp[i] + 0;//arry[j - 1 - i] = tmp[i] + 1;//}//return arry;//}There is a string of beads 2 colors (red, blue), N (n is odd), how many blue beads can be determined no matter what order to wear//can be from different positions of two blue beads//truncated to 2 strings, and get a bunch of numbers for (n+1)/2 (not including these two Blue beads)//heavy in logical understanding Code Simple Void pearl_num (int n) {if (n < 3 | | n % 2 != 1) return;int head = 0,end=0,count=0,num=2;for (int i = 1; i <n-2; ) {end = i;if (end == (n + 1) { / 2) {printf ("%d\n", num); return;} if (end - head - 1) == (n + 1) / 2 | | (n - 1 - end) == (n + 1) / 2) {i++;} else if (count > 0) {count--;i++;} else{num++;count=num-2;i = 1;}} printf ("No answer! \ n ");} Numbers of man reading 0<=num<1000 for example input 101 output yibailingyi void test () {int num = 0;char* a[12] = { "Ling", "yi", "er", "san", "Si", "WU", "Liu", "Qi", " Ba ", " Jiu ", " Shi ", " Bai " };while (1) {Int arr[3] = { 0 };char * pinyin = (char*) malloc (sizeof (char) *20); scanf ("%d", &num); int tmp1 = num;for (int i = 2; i >= 0; --i) {arr[i] = num % 10;num /= 10;} char* tmp = pinyin;for (int i = 0; i<3; ++i) {if (TMP1 == 0) {strcpy (pinyin, "Ling\0");p Inyin += 4;break;} if (i == 0) {if (arr[i] != 0) {strcpy (Pinyin, a[arr[i]]);p inyin += (int) strlen (a[arr[i]); strcpy (pinyin, a[11]);p inyin += 3;}} else if (i == 1) {if (arr[i] == 0) {if (arr[i - 1] == 0) continue;if (arr[i + 1] == 0) break;strcpy (pinyin, a[0]);p inyin += 4;} else{strcpy (Pinyin, a[arr[i]);p inyin += (int) strlen (a[arr[i]); strcpy (pinyin, a[10]);p inyin += 3;}} else{if (arr[i] != 0) {strcpy (pinyin, a[arr[i]);p inyin += (int) strlen (A[arr[i]]) ;}} free (pinyin);} *pinyin = 0;printf ("%s\n", tmp);p inyin = tmp;}} Xiao Ming outfit equipment according to from left to right if the item is too large (greater than the maximum capacity of the backpack) directly discarded, if the backpack can not fit //to replace a backpack, the previous backpack no longer use, how many items can be loaded Xiao Ming? n Items t Backpack weight m backpack number//a[0]--a[n] Given item quality//void package () {int m, t &NBSP;,N;INT&NBSP;A[20]&NBSP;=&NBSP;{&NBSP;0&NBSP;};INT&NBSP;I&NBSP;=&NBSP;0;SCANF ("%d%d%d", &n,&t, &m);while (i < n) {scanf ("%d", &a[i++]);} int count = 0, tmp = t;for (i=0; m>0 && n > 0;) {if (a[i] > tmp) {i++;n++;} else if (t-a[i]>=0) {t -= a[i++];++count;--n;} else if (m>1) {--m;t = tmp;t -= a[i++];++count;--n; } Else{++i;--n;}} printf ("%d\n", count);} String Press space Flip Simultaneous case reversal Example: "This is a str"-> "str a Is this "//default given string contains only letters and spaces Void reverse_str_a (CHAR&NBSP;*&NBSP;SRC) {char* str = (char*) malloc (sizeof (char) * (strlen (SRC)));//Copy the given string to the strcpy (STR,SRC) in an array of uppercase characters; char* tmp = str;int i = 0;char* out = (char*) malloc (sizeof (char) * (strlen (str) ));//Generate stored processed character array char* output = out;while (* (TMP))//Calculate the number of spaces {if (*tmp++ == ' ' &&*tmp!= 0)//' no calculation before ++i;} int count = i;int *a = (int *) malloc (sizeof (int) *i);//Generate a record space subscript array tmp = str;i = 0;int j = 0;while (*TMP)//The space subscript is recorded and replaced with ' ' for easy copy {if (*tmp = = ' ')//{*tmp = 0;if (* (tmp + 1) == 0)//' + ' before the space directly processed Replace the first character of the output array with a space point to the next {*output = ' '; output++;break;} A[j++] = i;} ++tmp;++i;} tmp = str;for (&NBSP;J&NBSP;=&NBSP;COUNT-1;&NBSP;J>=0;&NBSP;--J)//writes the split string from the back to the output string and the respective \ 0 Replace with space {if (a[0] == 0&&j==0) break;strcpy (output,tmp+a[j]+1); Output += strlen ( TMP&NBSP;+&NBSP;A[J]+1); * (output++) = ' ';} if (a[0] == 0)//Processing start space tmp++;strcpy (output, tmp );//Copy the first string to the tail output += Strlen (tmp );if (a[0] == 0)//move the opening space to the end {output[0]= ' '; output[1] = 0;} i = 0;output = out;while (* (output+i))//Replace Case {if (* (output + i) >= ' A ' &&* (output + i) <= ' z ') * (output + i) -= 32;else if (* (output + i) >= ' A ' &&* (output + i) <= ' Z ') * (Output + i) += 32;++i;} cout << out << "***********" << endl;//output free (out); FREE (output); free (a);} Int main () {cout<<cownum () <<endl;cout << cownum1 () << endl ; Allcolorbead ("11231324566457711234567111", 7); Allcolorbead ("ASDCCAACSDFDGFWERERDFG", 8); Snakearr (5);char* a = "Vvaabbcdcbbaaxaabbcdcbbaax"; Cout << plalindromenum (A, 0, strlen (a)) << endl;/*pearl_num (101);p ackage (); */reverse_str_a (" This w W w Is str ");/* Test (); *//*char * aa; aa=gray ( cout << aa << endl;*/); return 0;}
C + + Some pen questions