嵌入式软件c语言笔试题

收集的一些嵌入式软件C语言题目,代码均已经过验证正确无误

1、CPU大小端问题

#include 

void main()
{
	union  
    {  
        int a;  
        char b;  
    }c;  
    c.a = 1;
	 
	int d;
	d = c.b;
	if(d == 1)
		printf("CPU是小端,c.b = %d\n",c.b);
	if(d == 0)
		printf("CPU是大端,c.b = %d\n",c.b);
	return;
}

2、递归
求阶乘n!

int function1(int n)
{
	if (n == 0)
		return 1;
	while (n >= 1)
	{
		return n*function1(n - 1);
	}
}

int function2(int n)
{
	if (n == 0)
		return 1;
	else
		return n*function2(n - 1);
}

3、输入一个整数(任意位数),倒叙输出:

//计算位数
int length_num(int n)
{
	int num_temp = n;
	int flag = 0;
	while (num_temp > 0)
	{ 
		num_temp /= 10;
		flag++;
	}
	return flag;
}
//输入一个整数(任意位数),倒叙输出:
int fun3(int n)
{
	int flag = length_num(n);
	int *p = (int *)malloc(flag * sizeof(int));
	for (int i = 0; i < flag; i++)
	{
		int temp = n % 10;
		*(p + i) = temp;
		n = n / 10;
	}//例如n=123,存储为321
	int n1 = 0;
	for (int i = 0; i < flag; i++)
	{
		n1 = 10 * n1 + (*(p + i));
	}
	free(p);
	p = NULL;
	return n1;
}

4、将一个数组逆序放到原来数组中。(考虑复杂度)

void fun4(int *p, int length)
{
	int n = length / 2;
	for(int i=0;i

5、斐波那契数列:
Fib(0) = 0
Fib(1) = 1
Fib(n) = Fib(n-1) + Fib(n-2)

F() = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

//斐波那契,使用递归算法复杂度为 O(2n)。
int fun5(int n)
{
	if (n == 0)
		return 0;
	if (n == 1)
		return 1;
	if (n > 1)
		return fun5(n - 1) + fun5(n - 2);
}
//斐波那契,不用递归,算法复杂度O(n)
int fun6(int n)
{
	if (n <= 1)
		return n;
	else
	{
		int forward1 = 0, forward2 = 1;
		int sum = 0;
		for (int i = 1; i < n; i++)
		{
			sum = forward1 + forward2;
			forward1 = forward2;
			forward2 = sum;
		}
		return sum;
	}
}

6、排序
6.1冒泡排序

//冒泡排序,顺序 小---大,比较相邻数,每次循环后,最后的数总是最大
void sort1(int *a, int N)
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N-1 - i; j++)
		{
			
			if (a[j + 1] < a[j])
			{
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}//j
	}//i
}

6.2、选择排序

//选择排序,每次选出i-N中最小值
void sort2(int *a, int N)
{
 
	for (int i = 0; i < N; i++)
	{
		int min_flag = i;
		for (int j = i; j < N; j++)
		{
			if (a[min_flag] > a[j])
			{
				min_flag = j;
			}
 
		}
		int temp = a[i];
		a[i] = a[min_flag];
		a[min_flag] = temp;
	}
}

7、字符串循环移动
编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh” 函数头是这样的:

//pStr是指向以’\0’结尾的字符串的指针
//steps是要求移动的n
void LoopMove ( char * pStr, int steps )
{
//请填充…
}

//移动字符串循环1
void str_loop(char *p,int N,int loop_n)
{
	//求数组长度,包括\0
	int length = 0;
	for (int i = 0; i < N; i++)
	{
		length++;
		if (p[i] == '\0')
			break;
	}
	char * temp;
	temp = (char*)malloc(length * sizeof(char));//求loop后的temp
	loop_n = loop_n % (length - 1);
	int j = 0;
	for (int i =length-loop_n ; i < length - 1; i++)
	{
		temp[j] = p[i];
		j++;
	}
	for (int i = 0; i < length - loop_n; i++)//temp前半段赋值
	{
		temp[j] = p[i];
		j++;
	}	
	temp[j] = '\0';	
	for (int i = 0; i < length; i++)//temp后半段赋值
	{
		p[i] = temp[i];
	}
	free(temp);
}

8.写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于str2返回1,若str1小于str2返回-1

int strcmp(const char *src,const char *dst)
{
	int ret = 0;
	while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
	{
		src++;
		dst++;
	}
	if(ret<0)
		ret = -1;
	if(ret>0)
		ret = 1;
	return(ret);
}

9.已知strcpy函数的原型是:char * strcpy(char * strDest,const char * strSrc);
不调用库函数,实现strcpy函数。

char *strcpy(char * strDest,const char * strSrc)
{
	assert( (strDest != NULL) && (strSrc != NULL) );
	char *address = strDest; 
	while( (*strDest++ = * strSrc++) != '’ ); 
		return address;
}

10.二分查找算法:

A、递归方法实现:

int BSearch(elemtype a[],elemtype x,int low,int high)
/*在下界为low,上界为high的数组a中折半查找数据元素x*/
{
	int mid;
	if(low>high)
		return -1;
	mid=(low+high)/2;
	if(x == a[mid])
		return mid;
	if(x

B、非递归方法实现:

int BSearch(elemtype a[],keytype key,int n)
{
	int low,high,mid;
	low=0;high=n-1;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(a[mid].key==key)
			return mid;
		else if(a[mid].key

11.输入字符倒叙输出,输入’#'结束

#include

int main()
{
	char *ch;
	char a;
	int i = 0;
	puts("输入字符倒叙输出,输入'#'结束:");
	while((a = getchar()) != '#')
	{
		*ch = a;
		ch++;
		i++;
	}
	for(i;i>0;i--)
	{
		ch--;
		printf("%c",*ch);
	}
	printf("\n");
	return 0;
}

12.链表的逆序

#include
#include

typedef struct Node
{
	int data;
	struct Node *next;
}Node;

Node* LinkList()
{
	Node *head,*r,*p;
	int i = 1;
	head = (Node *)malloc(sizeof(Node));
	r = head;
	puts("输入节点数据,输入字符结束:");

	while(i)
	{
		p = (Node *)malloc(sizeof(Node));
		i = scanf("%d",&p -> data);
		if(i != 0)
		{
			r -> next = p;
			r = p;
		}
	}
	r -> next = NULL;
	return(head);
}

void print(Node* L)
{
	puts("print:");
	L = L -> next;
	while(L != NULL)
	{
		printf("%d ",L -> data);
		L = L -> next;
	}
	puts("");
}

void reverseOutputLinklist(Node* L)
{
	Node *r,*p;
	r = L -> next;
	L -> next = NULL;
	while(r != NULL)
	{
		p = r;
		r = r -> next;
		p -> next = L -> next;
		L -> next = p;
	}
}

void main()
{
	Node* a;
	a = LinkList();
	print(a);
	reverseOutputLinklist(a);
	puts("reverseOutputLinklist");
	print(a);
	return;
}

13.快速排序

#include 

void swap(int *a,int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

void quicksort(int array[],int begin,int end)
{
	int i = begin;
	int j = end;
	int pivotkey;
	
	if(begin < end)
	{
		pivotkey = array[i];
		while(i < j)
		{
			while((i < j)&&(array[j] >= pivotkey))
				j--;
			swap(&array[i],&array[j]);
			while((i < j)&&(array[i] <= pivotkey))
				i++;
			swap(&array[i],&array[j]);
		}
		
		quicksort(array,begin,i);
		quicksort(array,i+1,end);
	}
}

void main()
{
	int i = 0;
	int a[15] = {6,5,8,1,16,3,19,87,56,4,64,11,24,52,7};
	for(i = 0;i < 15;i++)
		printf("%d ",a[i]);
	printf("\n");
	quicksort(a,0,14);
	for(i = 0;i < 15;i++)
		printf("%d ",a[i]);
	printf("\n");
}

14.16进制字符串转10进制数

#include
#include

int fun(char *a)
{
	int len = 0;
	int i = 0;
	long b = 0;
	while((*(a+len)) != '\0')
	{
		len++;
	}
	for(;i='a'&&(*(a+i))<='z')
			b = (b*16)+((*(a+i))-('a')+10);
		if((*(a+i))>='0'&&(*(a+i))<='9')
			b = (b*16)+((*(a+i))-('0'));
	}
	return(b);
}

void main(void)
{
	int a,c,len,i;
	char b[10];
	scanf("%x",&a);
	sprintf(b,"%x",a);
	c = fun(b);
	printf("%d\n",c);
}

15.字符串压缩
eg:输入“aassssdccccvvttt”,输出“2a4sd4c2v3t”

#include 
#include

int count(int j);
char zip(char *stri,int len,char *stro);

void main()
{
	int len;
	char stri[100]={},stro[100]={};
	scanf("%s",stri);
	len = strlen(stri);
	zip(stri,len,stro);
	printf("%s\n",stro);
}

int count(int j)
{
	int count = 0;
	while(j>0)
	{
		count++;
		j/=10;
	}
	return(count);
}

char zip(char *stri,int len,char *stro)
{
	int i = 0,k = 0;
	int j = 1;
	char flag = 0;
	
	while(i1)
		{
			sprintf((stro+k),"%d",j);
			k = k+count(j);
			j = 1;
		}
		*(stro+k) = flag;
		k++;
	}
}

16、不使用 if,swtich,?: ,输出a,b中的较大值

//a、b两者之和加上a、b的差值除以二即为a、b中的较大值

#include

int fun(int a,int b)

{
	int sum = a+b;
	int dif = sab(a-b);	//sab()为求绝对值函数
	int max = (sum+dif)/2;

	return(max);
}

17、x&(x-1);//统计二进制中1的个数

你可能感兴趣的:(编程语言)