嵌入式軟體c語言筆試題
阿新 • • 發佈:2018-12-20
這段時間參見嵌入式c筆試,對筆試中的一些c語言題目進行總結收錄。
1、CPU大小端問題
#include <stdio.h>
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); return n1; }
4、將一個數組逆序放到原來陣列中。(考慮複雜度)
void fun4(int *p, int length)
{
int n = length / 2;
for(int i=0;i<n;i++)
{
int temp = *(p + i);
*(p + i) = *(p + length - 1 - i);
*(p + length - 1 - i) = temp;
}
}
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<a[mid])
return(Bsearch(a,x,low,mid-1));
else
return(BSearch(a,x,mid+1,high));
}
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<key)
low=mid+1;
else
high=mid-1;
}
return -1;
}
11.輸入字元倒敘輸出,輸入’#'結束
#include<stdio.h>
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<stdio.h>
#include<malloc.h>
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 <stdio.h>
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<stdio.h>
#include<string.h>
int fun(char *a)
{
int len = 0;
int i = 0;
long b = 0;
while((*(a+len)) != '\0')
{
len++;
}
for(;i<len;i++)
{
if((*(a+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 <stdio.h>
#include<string.h>
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(i<len)
{
if((*(stri+i)) != flag)
{
flag = *(stri+i);
i++;
}
while(flag == *(stri+i))
{
j++;
i++;
}
if(j>1)
{
sprintf((stro+k),"%d",j);
k = k+count(j);
j = 1;
}
*(stro+k) = flag;
k++;
}
}