嵌入式软件笔试常见编程题

排序算法

快速排序

/* 选择数组的一个数作为基数,一次排序完使得 
 * 基数左边全是小于基数; 基数右边全是于基数*/
void quick_sort(int a[], int start, int end)
{
    
    
	if(start >= end){
    
    
		return ;
	}
	int x = start;
	int y = end;
	int base = a[start];
	
	while(x < y){
    
    
		while(a[y] > base && x < y){
    
    
			y--;
		}
		if(x < y){
    
    
			a[x++] = a[y];
		}
		while(a[x] < base && x < y){
    
    
			x++;
		}
		if(x < y){
    
    
			a[y--] = a[x];
		}
	}
	a[x] = base;
	quick_sort(a, start, x-1);
	quick_sort(a, x+1, end);
}

冒泡排序

void Bubble_sorting(int *a, int len)
{
    
    
    int i = 0,j = 0;
    int temp;
    for(i=0; i<len-1; i++){
    
    
    /* 此层循环选择一个大的数放后面 */ 
        for(j=0; j<len-1-i; j++){
    
    
            if(*(a+j) > *(a+j+1)){
    
    
                temp = *(a+j+1);
                *(a+j+1) = *(a+j);
                *(a+j) = temp;
            }
        }
    }
}

简单选择排序

/* 记下头一个数的下标与数字
 * 再向后面的数组找比它小的数
 * 记下下标与数字,然后换掉  */
void Simple_choice(int a[], int length)
{
    
    
	int i,j,num,index;
	for(i=0; i<length; i++)
	{
    
    
		num = a[i];
		index = i;
/* 此层循环 在i后面的数组找到比num小的数 */
		for(j=i+1; j<length; j++)
		{
    
    
			if(a[j]<num)
			{
    
    
				num = a[j];
				index = j;
			}
		}
/* 当找到索引就会变化, 就需交换这两个数 */
		if(i != index)
		{
    
    
			a[index] = a[i];
			a[i] = num;
		}
	}
}

直接插入排序

void zhicha(int a[], int length)
{
    
    
	int i,j,num;
	for(i=1; i<length; i++)
	{
    
    
		num = a[i];
	/* 此层循环把num插入前面的序列中
	   插完0-i已经排好序            */
		for(j=i-1; j>=0; j--)
		{
    
    
			if(a[j] > num)
			{
    
    
				a[j+1] = a[j];
			}
			else{
    
    
				break;
			}
			a[j] = num;
		}
	}
}

希尔排序

void shell_sort(int a[], int length)
{
    
    
	int i,j,h, num;
	for(h=length/2; h>0; h/=2)
	{
    
    
		for(i=h; i<length; i++)
		{
    
    
			num = a[i];
			for(j=i-h; j>=0; j-=h)
			{
    
    
				if(a[j] > num)
				{
    
    
					a[j+h] = a[j];
				}
				else{
    
    
					break;
				}
			}
			a[j+h] = num;
		}
	}
}

二分查找

/* 
参数:数组,数组元素个数,要查找的数
返回:成功找到返回元素下标;失败返回-1
*/
int binary_search(int a[], int len, int num)
{
    
    
	int low = 0, high = len-1;
	int mid = (low+high) / 2;
	
	while(low <= high){
    
    
		if(num == a[mid]){
    
    
			return mid;
		}
		else if(num > a[mid]){
    
    
			low = mid + 1;
			mid = (low+high) / 2;
		}
		else{
    
    
			high = mid - 1;
			mid = (low+high) / 2;
		}
	}
	return -1;
}

数字反转

int turn_number(int num)
{
    
    
	int ret = 0;
	int yu;
	
	while(num){
    
    
		yu = num % 10;
		ret = yu + 10*ret;
		num = num / 10;
	}
	return ret;
}

字符串反转

char *str_reversal(char *p_str)
{
    
    
	if(NULL == p_str)
		return NULL;
	char *pHead = p_str;
	char *pTail = p_str + strlen(p_str)-1;
	char temp;
	
	do{
    
    
		temp = *pHead;
		*pHead = *pTail;
		*pTail = temp;
		pHead++;
		pTail--;
	}while(pHead <= pTail);
	return p_str;
}

字符串转换数字

int stoi(char *p)
{
    
    
	int i = 0;
	for(i = 0; *p; p++){
    
    
		i = (*p - '0') + 10 * i;
	}
	return i;
}

求一个数中1的位数

算法:让一个数的最低位和1位与,位与完数向右移1位,如果与的结果位1,计数+1。返回计数值。


int NumOne(int i)
{
    
    
	int ret = 0;
	while(i){
    
    
		if(i & 1)
			ret++;
		i = i>>1;
	}
	return ret;
}

判断一个数是否为2的幂

算法:2的次幂的二进制特征是最高位为1,其余全0。可以用它和(它-1)位与,结果为0表示是,反之不是。

/* 传一个整数,当这个数为2的幂时返回0;不是返回1 */
int judge(int i)
{
    
    
	if(i <= 0)      //2的次方都是正数
		return 1;
	else{
    
    
		if(!(i & (i-1)))
			return 0;
		else
			return 1;
	}
}

实现strcat

char *my_strcat(char *dest, const char *src)
{
    
    
	char *p = dest;
	if(NULL == dest || NULL == src){
    
    
		return NULL;
	}
	while(*dest){
    
    
		dest++;
	}
	while(*dest++ = *src++);
	return p;
}

实现strcmp

int my_strcmp(const char *s1, const char *s2)
{
    
    
	for(; *s1 == *s2; s1++,s2++){
    
    
		if(*s1 == '\0')
			return 0;
	}
	return *s1-*s2;
}

实现strcpy

char *my_strcpy(char *dest, const char *src)
{
    
    
    char *p = dest;
    if(NULL == dest || NULL == src){
    
    
        return NULL;
    }
    while((*dest++ = *src++) != '\0');
    return p;
}

实现memcpy

void *my_memcpy(void *dest, const void *src, size_t count)
{
    
    
	char *pdest = dest;
	const char *psrc = src;
	
	while(count--){
    
    
		*pdest++ = *psrc++;
		
	}
	return dest;
}

实现memmove

void *my_memmove(void *dest, const void *src, size_t count)
{
    
    
	char *tmp;
	const char *s;

	if((dest + count < src) || (src+ count) < dest)){
    
      //两个内存没有重叠
		tmp = dest;
		s = src;
		while (count--)
			*tmp++ = *s++;
	} else {
    
    
		tmp = dest;
		tmp += count;
		s = src;
		s += count;
		while (count--)
			*--tmp = *--s;
	}
	return dest;
}

实现atoi

描述:字符串转换成数字,前面跳过空格,当第一个字符为+、-、数字字符时,函数转换,直到非数字字符,返回转换后的整型数。若非空格后的第一个字符不是上面三者其一,则返回0。

int my_atoi(const char s[])
{
    
    
	int i;
	int sign;
	int ret = 0;
	
	for(i = 0; isspace(s[i]); i++);  //isspace()跳过空格
	sign = (s[i] == '-') ? -1 : 1;

	if(s[i] == '+' || s[i] == '-'){
    
    
		i++;
	}
	for(ret = 0; isdigit(s[i]); i++)  // isdigit()判断是否是数字
		ret = (s[i]-'0') + ret*10;
	return ret * sign;
}

转自https://blog.csdn.net/weixin_42934918/article/details/120191245

猜你喜欢

转载自blog.csdn.net/qq_44333320/article/details/129193053