//print 1 to the maximum number of n digits. //title: Enter the number n, in order to print out the n decimal number from 1 to the maximum. For example://Enter 3, print 1, 2, 31 up to the maximum 3-digit 999.//[Trap]: The easiest way to think about this topic is to find the maximum number first. Then the loop output is ready. #include <iostream>#include <string.h>using namespace STD;voidGrial (Char*STR,Char*s) {if(*s = =' + ') {cout<< str << Endl;return; }Else{ for(inti =0; I <=9; i++) {grial (str,s+1); *s =' 1 '+ i; } }}voidGrial (intx) {Char*s =New Char[x];memset(S,' 0 ',sizeof(s)); * (s + x) =' + ';Char*str = s; Grial (str, s);}intMain () {grial (2);return 0;}
#include <iostream>using namespace Std;voidGrial (Char*Str,intN) {int Count=1;Char*p =Str+n; while(Count!=0&& *Str!=' 1 ') {if(*p-' 0 '+Count>=Ten) {*p =' 0 ';Count=1; }Else{*p + =Count;Count=0; } p--; }}voidGrial (intx) {Char*Str=New Char[x +1]; MemsetStr,' + ', sizeof (Str)); for(inti =0; I <= x; i++) {strcat (Str,"0");//Open one more, used as a condition for terminating judgment. }Char*p =Str; while(1) {p =Str; Grial (P,X); p =Str; while(*p = =' 0 ') p++;if(*Str==' 1 '&& p = =Str) Break;//Terminate position. cout << p << Endl; }}intMain () {grial (3);return 0;}
#include <iostream>usingnamespace Std;/*3. Number of positive points: the implementation function double power (double base, int exponent), the exponent of the base, must not use the library function, do not need to consider the large number of problems. Note: Consider the return of illegal input. */DoubleGetsum (Double Base,intExponent) {DoubleCount =1; while(exponent) {Count *=Base; exponent--; }returnCount;}DoublePowerDouble Base,intExponent) {if(Base==0)return 0;if(Exponent = =0)return 1;DoubleCount =1;if(Exponent >0) {count = Getsum (Base, exponent); }Else{count =1/getsum (Base,-exponent); }returnCount;}intMain () {cout << power (2, -2) << Endl;return 0;}
#include <iostream>using namespace STD;//The number of occurrences more than half the number in the array. intGrial (intA[],intLowintHighintM) {inti = low;intj = high;if(I >= J)return 0;intkey = A[i]; while(I < J) { while(I < J && A[j] > key) j--; A[i] = A[j]; while(I < J && A[i] < key) i++; A[J] = A[i];if(I<J) {i++; j--; }} A[i] = key;if(I >= M) {returnA[i]; }Else if(i<m) {returnGrial (A, i +1, high, M); }Else{returnGrial (A, low, I-1+ j); }}intMain () {//int a[] = {0,1,1,2,1,1,2}; intA[] = {0,1,1,1,1,1,1,4,2,3,4};intn =sizeof(a)/sizeof(int);intMid = (n%2==0) ? (N/2+1): N/2;cout<< Grial (A,0, N-1, mid) << Endl;return 0;}#include <iostream>using namespace STD;//The number of occurrences more than half the number in the array. intGrial (intA[],intN) {intCount =0;intVal for(inti =0; I < n; i++) {if(Count = =0) {val = a[i]; }if(val = = A[i]) {count++; }Else{count--; } }returnVal;}intMain () {intA[] = {0,1,1,1,1,1,1,4,2,3,4};cout<< Grial (A,sizeof(a)/sizeof(int));return 0;}
/* Adjust array so that all odd numbers are in front of even numbers. Title: Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits in the array are in the first half of the array, and all the even digits are in the second half of the array.*//* #include<iostream>using namespace Std;void grial (intA[],intN) {inti =-1;intj =0; while(j<n) { while(A[j]%2==0) j + +; i++;if(j = = N) Break;if(I! = j) {A[i] ^= a[j]; A[J] ^= a[i]; A[i] ^= a[j]; } j + +; }}intMain () {intA[] = {4,5,3,1,4,6,7,8,0,6,5643,5,6,1}; Grial (A, sizeof (a)/sizeof (int)); for(inti =0; I < -; i++) {cout << a[i] <<" "; } cout << Endl;return 0;}
/*3. Number of occurrences in the sorted array. Title: Counts the number of occurrences of a number in a sorted array. For example: Sort array {1,2,3,3,3,3,4,5} and number 3, because 3 appears 4 times, so the output 4.*/#include <iostream>using namespace STD;intGrial (intA[],intNintVal) {//two points to find. inti =0;intj = N-1;intMidintCount =0; while(I <= J) {mid = (i + j)/2;if(A[mid]>val) {j = mid-1; }Else if(A[mid] < val) {i = mid +1; }Else{i = mid-1; j = Mid +1; count++; while(a[i--] = = val) count++; while(a[j++] = = val) count++;returnCount } }return-1;}intMain () {intA[] = {1,2,3,3,3,3,4,5};cout<< Grial (A,sizeof(a)/sizeof(int),5) << Endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + face question a big wave