voidSwapA(int&A,int&B ){if( A == B ){return;}
A = A + B;
B = A - B;
A = A - B;}voidSwapB(unsignedint&A,unsignedint&B )//变量进行或运算{if( A == B ){return;}
A = A ^ B;
B = A ^ B;
A = A ^ B;}
5.实现一个将字符串逆序的方法 (轴对称两两置换)
char*MyInverted(char*pDest ){assert(nullptr!= pDest );constint Len =strlen( pDest );char T =0;for(int i =0; i < Len /2;++i ){
T = pDest[ i ];
pDest[ i ]= pDest[ Len - i -1];
pDest[ Len - i -1]= T;}return pDest;}
6.C语言中字符串转数字的方法是什么( atoi ),请实现它 (倒叙算差值,求累加值)
intMyatoi(constchar*pString ){assert(nullptr!= pString );constint Len =strlen( pString );int Value =0;int Times =1;for(int i = Len -1; i >=0;--i, Times *=10){
Value +=( pString[ i ]-'0')* Times;}return Value;}
intBinarySearch(int*pArray,int Count,int Value ){assert(nullptr!= pArray );int Left =0;int Right = Count -1;int Mid =0;while( Left <= Right ){
Mid =( Left + Right )/2;if( Value < pArray[ Mid ]){
Right = Mid -1;}elseif( Value > pArray[ Mid ]){
Left = Mid +1;}else{return Mid;}}return-1;}
intGetDay(int Year,int Month,int Day ){int MonthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};if(( Year %4==0&& Year %100!=0)||( Year %400==0)){++MonthDays[2];}int Days =0;for(int i =1; i < Month;++i ){
Days += MonthDays[ i ];}
Days += Day;return Days;}
14.递归求斐波拉契数列数列第N项
intGetFibonacci2(int N ){if(1== N ||2== N ){return1;}returnGetFibonacci2( N -1)+GetFibonacci2( N -2);}
15.实现一个产生[N-M]区间数字的随机方法
intGetRandomRange(int N,int M ){if( N == M ){return N;}if( N > M ){
N = N + M;
M = N - M;
N = N - M;}return N +(rand()%( M - N +1));}
voidCountLetter(constchar*pSrc ){int count[256]={};for(;*pSrc !='\0';++pSrc ){constchar&c =*pSrc;if(( c <'A'|| c >'z')&&( c <'a'|| c >'z')){continue;}++count[ c ];}}