吉大C语言期末考试题

2016级《程序设计基础》期末考试题

1.将整型数组a中所有元素逆序存放,函数声明为:void rev(int a[],int n)

#include
int main(){
    void rev(int a[],int n);
    int a[6] = {1,2,3,4,5,6};
    rev(a, 6);
}
void rev(int a[],int n){
    int i,t;
    for(i=0;i

 

2.比较两个字符串,返回其长度的差值,函数声明为int comp(char *a,char *b)

 

注:不许用string.h头文件

#include
#include 
int main(){
    int comp(char *a,char *b);
    char a[100];
    char b[100];
    gets(a);
    gets(b);
    printf("%d\n",comp(a, b));
}
int comp(char *a,char *b){
    int i=0;
    int na,nb;
    while(1){
        if(a[i]=='\0'){
            na = i;
            break;
        }
        i++;
    }
    i = 0;
    while(1){
        if(b[i]=='\0'){
            nb = i;
            break;
        }
        i++;
    }
    return fabs(na - nb);
}

 

3.一个长度为n(double,n>5)的木条,将其折断为2n/5,3n/5的两木条;若折断后的木条长度大于5,则继续按上述方法折断,

 

直到得到的木条不大于5。编写递归函数,计算一个长度为n的木条,最后折断为多少根木条。

#include
int main(){
    int broke(double n,int time);
    double n;
    scanf("%lf",&n);
    printf("%d\n",broke(10,1));
}
int broke(double n,int num){//n长度,num木条根数
    num++;//折断
    
    if(n*2/5 > 5){
        num =  broke(n*2/5,num);
        num =  broke(n*3/5,num);
    }else{
        if(n*3/5 > 5)
            num =  broke(n*3/5,num);
    }
    return num;
}

4.写学生的信息卡片

#include
#define N 5
#define M 11
#define LEN sizeof(struct student)
struct student{
    int id;
    char name[N];
    char  phone[M];
    struct student *next;
};
int main(){
    struct student *creatList();
    struct student *p = creatList();
    printf("学生信息为:\n");
    while(p!=NULL){
        printf("id=%d,name=%s,phone=%s\n",p->id,p->name,p->phone);
        p = p->next;
    }
}
struct student *creatList(){
    struct student *head;
    struct student *p,*q;
    p=q=(struct student *)malloc(LEN);
    head = NULL;
    int n = 0;
    printf("请输入第%d个学生的学号:",n+1);
    scanf("%d",&p->id);
    printf("请输入第%d个学生的姓名:",n+1);
    scanf("%s",&p->name);
    printf("请输入第%d个学生的电话号码:",n+1);
    scanf("%s",&p->phone);
    while(1){
        n++;
        if(n==1) head = p;
        else q->next = p;
        q = p;
        p=(struct student *)malloc(LEN);
        printf("请输入第%d个学生的学号:",n+1);
        scanf("%d",&p->id);
        if(p->id==-1) break;//学号输入-1则停止
        printf("请输入第%d个学生的姓名:",n+1);
        scanf("%s",&p->name);
        printf("请输入第%d个学生的电话号码:",n+1);
        scanf("%s",&p->phone);
    }
    q->next = NULL;
    return head;
}

 

你可能感兴趣的:(C,期末题)