C 2014 Nian questions pen

1, points out errors in the program, explain the reasons and correct

int *p,*q;
p=malloc(sizeof(int)*20); 
q=malloc(sizeof(int)*10);
…
q=p;
…
free(p);
free(q);

analysis:

Error 1, q originally stored address an int type of memory area, in the absence of the release of this address to modify the contents of q, so that this area can not be recovered, resulting in a memory leak .

Error 2, p and q refer to the same area of memory, the memory region has been released after using free (P), can not be free again. Correction: The Free (Q) placed in front of q = p (correction of different ways, one selected here)


2, pointed out errors in the program, explain the reasons and correct
// string exchange 
void the swap (P char *, char * q) 
{ 
	char * TEMP; // P, the exchange value q refer 
	* TEMP = P *; 
	* P = q *; 
	* q * = TEMP; 
}

Analysis: The code first creates a parameter contents p and q takes two char type of address, within the code to exchange these two parameters, without any impact on the outside world.

The code is modified as follows:

the swap void (P char *, char * Q) 
{ 
	char TEMP [100]; 
	strcpy (TEMP, P); // strcpy function: copy string 
	strcpy (P, Q); 
	strcpy (Q, TEMP); 
}

3, short answer, arr integer array, N is the length of the array, enumb of integer variables, the following function is responsible for identifying arr array equal enumb position of the element. Three kinds of anomalies noted that the program, and why.

for(i=N-1;arr[i]!=enumb;--i)
  printf(“%d”,i);

analysis:

First, for the back loop should have a semicolon, otherwise each cycle are output.

Second, if the array arr no enumb, i will become a negative number, array overflow.

Finally, if i have been reduced, i will decrease until it overflows.


 4. The short answer questions, IF the else s1 s2 (B); What is the structure? Use shows the structure of language how to express? And mark conditional jump and jump force

 Analysis: conditional branching structure

if (b) goto L1; // conditional jump 
goto L2; // Forced Jump 
Ll: S1 
L2 of: S2

5、简答题,C语言中,常量存储在哪里?静态局部变量和静态全局变量存储在哪里?

分析:常量存储在常量区,静态局部变量和静态全局变量存储在全局数据区。


 6、填空题,下面程序是对链表进行选择排序,填上空缺的部分

list selectsort(list head){
	list p = (struct node*)malloc(sizeof(node));
	p->next = head;
	head = p;//新建一个头结点
	p = head->next;
	list k = head, q = head;

	while (p->next)
	{
		q = p->next;
		k = q;  //k记录最小值位置
          //找到最小元素位置 while (q) { if (q->data < k->data) { k = q; } q = q->next; }
          //交换元素位置 if (p->next != k) { int r = p->next->data; p->next->data = k->data; k->data = r; } p = p->next; //填空一 } p = head; head = head->next; //填空二 free(p); //释放头结点 return head; //填空三 } 

 7、填空题,快速排序法求某个数组前n个元素第k大的数

int find_k(int *a, int n,int k) {
	int i, j, t;
	int low = 0,high = n - 1;
	do {
		i = low; j = high; t = a[low];
		do {
			while (a[j]>t) j--;
			while (a[i]<t) i++;
			if () 
				swap(&a[j], &a[i]);
			else
				;
		} while (i<j);
		if (i == k)
			return t;
		if (i>k)
			;
		if (i<k)
			;
	} while (low<high);
	;
}

分析:该题目使用快排划分的第一种写法,原理是利用每次快排划分之后总能确定一个元素的最终位置。该程序可能仍然存在问题,最大的数字是第0个。

int find_k(int *a, int n,int k) { //从大到小排序
	int i, j, t;
	int low = 0,high = n - 1;
	do {
		i = low; j = high; t = a[low];
		do {
			while (a[j]<t) j--;
			while (a[i]>t) i++;
			if (i<j) 
				swap(&a[j], &a[i]);
			else
				break;   //填空1
		} while (i<j);
		if (i == k)
			return t;
		if (i>k)
			high = i - 1;   //填空2
		if (i<k)
			low = i + 1;     //填空3
	} while (low<high);
	return  a[low];   //填空4
}

8、填空题,约瑟夫环问题 【见2015年】 

int a[N + 1];
int *p = a, i,j,k;
for(i = 0; i<N + 1; i++)
    *(p+i) = i;
;
;
for( i = 0; k != 1; p++)
{
    if()
        p = a + 1;
    if()
        i++;
    if()
    {
        k--;
        i = 0;
        *p = 0;
    }
}
for(i = 0; i<N + 1; i++)
{
    if()
        printf("%d\n",a[i]);
}

分析:15年约瑟夫环区别

int a[N + 1];
int *p = a, i,j,k;
//赋值0~N for(i = 0; i<N + 1; i++) *(p+i) = i; p = a + 1; //填空1 k = N; //填空2 for( i = 0; k > 1; p++) //k为剩下的人数 { if(p>a + N) p = a + 1; if(*p != 0) //碰到元素为0说明已经杀死,不参与计数 i++; //i报数 if(i == 3) { k--; i = 0; *p = 0; } }
//打印输出 for(i = 0; i<N + 1; i++) { if(a[i] != 0) printf("%d\n",a[i]); }

9、 填空题,完美乘法,若a*b=c,且abc中0~9的数字各出现一次,填写程序空缺处。

int f[10],s[3];
int n=0;
for (int a = 12; a<999; a++)
{
    int t = 0;
    //清空数字计数
    for (int x = 0; x<10; x++)
        f[x] = 0;
    for (int b = 345; b<9999; b++)
    {
        int c ;
        ;     //填空一
        s[0] = a;
        s[1] = b;
        s[2] = c;
        //计算abc中出现数字的次数
        for (int x = 0; x < 3; x++)
        {
            int y = s[x];
            while ()
            {
                int t = y % 10;
                f[t]++;
                ;    //填空二
            }
        }
        //检查是否每个数都个出现一次
        for (int x = 0; x<10; x++)
        {
            if ()  ;   //填空三
        }
        if ()
            printf("%d*%d=%d\n",a,b,c);
            ;//填空四 } }

分析:

int f[10],s[3];
int n=0;
for (int a = 12; a<999; a++)
{
    int t = 0;
    //清空数字计数
    for (int x = 0; x<10; x++)
        f[x] = 0;
    for (int b = 345; b<9999; b++)
    {
        int c = a*b;
        s[0] = a;
        s[1] = b;
        s[2] = c;
        //计算abc中出现数字的次数
        for (int x = 0; x < 3; x++)
        {
            int y = s[x];
            while (y > 0)
            {
                int t = y % 10;
                f[t]++;
                y = y / 10;
            }
        }
        //检查是否每个数都个出现一次
        for (int x = 0; x<10; x++)
        {
            if (f[x] != 1)  t++;
        }
        if (t == 0)
            printf("%d*%d=%d\n",a,b,c);
        n++;			//统计循环次数
    }
}

10、编程题,将字符串逆转,函数原型void reverse(char *str);,要求空间复杂度为O(1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char *str)
{
    int i = 0, len = strlen(str);
    for (; i < len / 2; i++)
    {
        char a = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = a;
    }
}
int main()
{
    char ch[] = "helloworld";
    reverse(ch);
    printf("逆转后:%s",ch);
    return 0;
}
 

11、编程题,比较两个身份证字符串的生日大小。函数原型int isbothsame(char str1[19], char str2[19]);

#include <stdio.h>
#include <stdlib.h>

int isbothsame(char str1[19], char str2[19])
{
    int low = 6, hight = 13;
    while (low<hight &&str1[low] == str2[low])low++; //low记录不相同的位置
    return str1[low] - str2[low];  //负数表示str1的年长
}
int main()
{
    char a[] = "412824199605281234",b[] = "41282519960825789x";
    if(isbothsame(a,b) < 0)
        printf("a年长!");
    else
        printf("b年长!");
    return 0;
}

12、编程题,计算1-x+x^2/2!-x^3/3!+…+x^n/n!  【见2015年】

void main()
{
    int n, x, j, i = 1;
    float sum = 1, k = -1;
    printf("Input n and x:\n");
    scanf("%d %d", &n, &x);
    while (i <= n) {
        k = -1;
        for (j = 1; j <= i; j++) {
            k = -1*k*x;
        }
        for (j = 1; j <= i; j++) {
            k =  k/ j;
        }
        sum += k;
        i++;
    }
    printf("%f\n", sum);
}

动态规划改进:

void main()
{
        int n,x,i=1;
        float sum = 1,k = -1;
        printf("Input n and x:\n");
        scanf("%d %d", &n, &x);
        while (i <= n) {
            k = -1 * k*x / i;
            sum += k;
            i++;
        }
        printf("%f", sum);
}

13、编程题一个链表,找出其中数据项最大的结点,然后将其移动到链表尾部(结点node由整型data节点指针next构成不允许申请新的结点

#include <stdio.h>
#include <stdlib.h>

typedef struct slist
{
    int data;
    struct slist *next;
};
void movemax(struct slist *L)
{
    struct slist *p = L->next, *pre = L, *max = p;
    //找到最大值的位置
    while (p)
    {
        if (p->data>max->data)max = p;
        pre = p;
        p = p->next;
    }
    //此时p指向NULL,pre指向最后一个结点
    //最大值和最后一个节点交换
    int temp = pre->data;
    pre->data = max->data;
    max->data = temp;
}
int main()
{
    //建立单链表【尾插法】
    int a[] = {1,4,7,8,5,2,9,6,3};
    struct slist *p,*head = (struct slist*)malloc(sizeof(struct slist));
    head ->next = NULL;
    int i;
    p = head;
    for(i = 0;i < 9;i++)
    {
        struct slist *node = (struct slist*)malloc(sizeof(struct slist));
        node ->data = a[i];
        node ->next = p ->next;
        p ->next = node;
        p = node;
    }
    printf("找到最大值后移:\n");
    movemax(head);
    //输出
    p = head ->next;
    while(p != NULL)
    {
        printf("%d\t",p ->data);
        p = p->next;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/pam-sh/p/12585160.html