String inversion and array parity division, reverse array parity

Source: Internet
Author: User

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] <"";}}*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.