String inversion and array parity division, reverse array parity
// Reverse the string, for example, 123 456 789 abc. The result is abc 789 456 123/* # include <iostream> # include <string. h> using namespace std; void Exchange (char * & str) {char * p = str + strlen (str)-1; char * q = str; while (q <p) {char temp = * q; * q = * p; * p = temp; q ++; p -- ;}} void Grial (char * str) {char * p = str; while (* p! = '\ 0') {char * p_1 = p; while (* p! = '') P ++; char * save = p + 1; * p = '\ 0'; Exchange (p_1); * p =''; p = save ;} exchange (str) ;}int main () {char s [] = "12345 1234 abc"; Grial (s); cout <s <endl; return 0 ;} // The principle is to reverse a single string (separated by spaces) in the entire string, such as abc 123. The first step is CBA 321, // In the second step, the entire string is reversed. After the operation, the result is 123 abc, which is used to reverse the string without changing the sequence of the original single string. * /// Baidu written test: given an array containing integers, rearrange the array so that the left side of the array is an odd number and the right side is an even number. // requirement: space complexity o (1 ), time complexity o (m ). /* # Include <iostream> using namespace std; void Grial (int a [], int x) {int I =-1; int j = 0; int temp; while (j <x) {while (j! = (X-1) & a [j] % 2 = 0) j ++; I ++; temp = a [I]; a [I] = a [j]; a [j] = temp; j ++; // place the even number in the I position each time, so the I position starts from-1, until j = x, the city on the left of I is odd, and the city on the Right of I is even .}} int main () {int a [] = {9, 6,}; Grial (a, 10); for (int I = 0; I <10; I ++) {cout <a [I] <"";}}*/