关于C语言题602

发布时间 2023-06-06 20:09:49作者: 风中凌乱的猪头

1、编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分割符),拧将统计结果依次保存在b指向的数组中。

 

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

#define N 10
int main()
{
    void count(char a[],char w[][N],int  n,int b[]);
    char a[N*4];
    char w[N][N];
    int n;
    int i = 0;
    int b[N];
    
    printf("please input the num of strings:");
    scanf("%d",&n);
    printf("please input some strings:");
    for(;i<n;i++)
    {
        scanf("%s",w[i]);
    }
    getchar();
    printf("please input a string:");
    scanf("%[^\n]",a);

    count(a,w,n,b);

    for(i = 0;i < n;i++)
    {
        printf("%4d",b[i]);
    }
    printf("\n");
    return 0;
    
}

void count(char a[],char w[][N],int n,int b[])
{
    char temp[N][N];
    int i = 0;
    int j = 0;
    int k = 0;
    int z = 0;
    int count;
    while(a[i] != '\0')
    {
        if((a[i] <= 'z'&&a[i] >= 'a')||(a[i] <= 'Z'&&a[i] >= 'A'))
        {
            temp[j][k] = a[i];
            k++;
        }
        else
        {
            j++;
            k = 0;
        }

        i++;
    }
    for(i = 0;i < n;i++)
    {
        count = 0;
        for(k = 0;k <= j;k++)
        {
            if(strcmp(w[i],temp[k])==0)
            {
                count++;
            }
        }
        b[z] = count;
        z++;
    }
}

2、 编写函数int stat(int a[],int n,int c[][2])a指向的数组中保存了由n1位整数组成的数列(n为偶数)。函数从前至后依次将a数组中每两个相邻元素拼成一个不超过2位的整数,从而生成有n/2个元素组成的整数数列;统计该数列中不同整数各自出现的次数,并将统计结果保存到c指向的二维数组中。函数返回不同整数的个数。

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

#define N 30

int main()
{
    int stat(int a[],int n,int c[][2]);
    srand((unsigned)time(NULL));
    int a[N];
    int n;
    int i;
    int c[N/3][2] = {0};
    int b;
    printf("please input a number of integer(even number):");
    scanf("%d",&n);
    
    for(i = 0;i < n;i++)
    {
        a[i] = rand()%10;
    }

    for(i = 0;i < n;i++)
    {
        printf("%4d",a[i]);
    }
    printf("\n");

    b = stat(a,n,c);
    printf("there are %d numbers\n",b);

    for(i = 0;i < b;i++)
    {
        printf("%d : %d\n",c[i][0],c[i][1]);
    }
    
    return 0;

}

int stat(int a[],int n,int c[][2])
{
    int i = 0;
    int j = 0;
    int k = 0;
    for(;i<n;)
    {
        a[j] = a[i]*10+a[i+1];//直接放入a数组中
        j++;
        i = i+2;

    }
    for(i = 0;i < n/2;i++)
    {
        j = 0;
        for(;j < k;j++)
        {
            if(a[i]==c[j][0])
            {
                c[j][1]++;
                break;
            }
        }
        if(j == k)//放在一个for循环里就很好使
        {
            c[k][0] = a[i];
            c[k][1]++;
            k++;
        }
        

    }
    return k;
}

3、编写函数fun(int *a, int n, int *odd, int *even)功能是:求出数组a[]中所有奇数之和以及所有偶数之和。并利用指针odd返回奇数之和,利用指针even返回偶数之和。 例如:a[]的值依次为:1923116;则利用指针odd返回奇数之和24;利用指针even 返回偶数之和 8

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

#define N 20

int main()
{
    void fun(int *a,int n,int *odd,int *even);
    srand((unsigned)time(NULL));
    int n;
    int a[N];
    int i;
    int odd = 0,even = 0;
    printf("please input number:");
    scanf("%d",&n);

    for(i = 0;i < n;i++)
    {
        a[i] = rand()%20;
    }
    for(i = 0;i < n;i++)
    {
        printf("%4d",a[i]);
    }
    printf("\n");

    fun(a,n,&odd,&even);

    printf("odd sum is %d , even sum is %d\n",odd,even);
}
void fun(int *a,int n,int *odd,int *even)
{
    int i;
    for(i = 0;i < n;i++)
    {
        if(a[i]%2)
        {
            *odd+=a[i];
        }
        else
        {
            *even+=a[i];
        }
    }
}

4、 程序功能:建立一个带有头结点的单向链表,并将存储在数组中的字符依次转储到链表的各个结点中。

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

#define N 20

typedef struct Lnode
{
    char data;
    struct Lnode *next;
}LNode;

int main()
{
    char a[N];
    int i = 0;
    LNode *L = (LNode *)malloc(sizeof(LNode));
    L->next = NULL;
    LNode *p;
    p = L;
    printf("please input a string:");
    scanf("%s",a);

    while(a[i] != '\0')
    {
        LNode *q = (LNode *)malloc(sizeof(LNode));// 尾插法;
        if(!q)
        {
            exit(0);
        }
        q->data = a[i];
        p->next = q;
        p = p->next;
        i++;
    }
    p->next = NULL;

    p = L->next;
    while(p)
    {
        printf("%c ",p->data);
        p = p->next;
    }
    printf("\n");

    return 0;
}

5、编写程序STUDENT *Create(STUDENT studs[],int n)STUDENT是一个结构类型,包含姓名、成绩和指针域。studs数组中存储了nSTUDENT记录。create函数的功能是根据studs数组建立一个链表,链表中结点按成绩降序排列,函数返回链表头指针。

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

#define N 20

typedef struct Lnode
{
    char name[20];
    int grade;
    struct Lnode* next;
}STUDENT;


int main()
{
    STUDENT *Create(STUDENT studs[],int n);
    STUDENT studs[N];
    STUDENT *p;
    int n;
    int i;
    printf("please input the number of students:");
    scanf("%d",&n);
    printf("please input the imformation:");
    for(i = 0;i < n;i++)
    {
        scanf("%s %d",studs[i].name,&studs[i].grade);
        getchar();
    }


   
    p = Create(studs,n);
   
    while(p)
    {
        printf("%20s%5d\n",p->name,p->grade);
        p = p->next;
    }
    printf("\n");


    return 0;

}

void INsert(STUDENT *L,STUDENT *s)
{
    STUDENT *p;
    STUDENT *q = NULL;
    p = L;
    s->next = NULL;
    while(p&&s->grade < p->grade)
    {
        q = p;
        p = p->next;
    }
    if(!p)
    {
        q->next = s;
    }
    else
    {
        q->next = s;
        s->next = p;
    }
}

STUDENT *Create(STUDENT studs[],int n)
{
    int i = 0;
    int max;
    int k;
    max = studs[0].grade;
    for(;i < n;i++)
    {
        if(max <= studs[i].grade)
        {
            max = studs[i].grade;
            k = i;
        }
    }
    studs[k].next = NULL;
    for(i = 0;i < n;i++)
    {
        if(i == k)
        {
            continue;
        }
        INsert(&studs[k],&studs[i]);
    }
    return &studs[k];
}

 

6、 题目:n个人围成一圈,顺序排号,从第一个开始报数(13报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.

 

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

#define OK 1
#define ERROR 0

typedef struct  Lnode
{
    int data;
    struct Lnode *next;
    struct Lnode *prior;
}LNode;

int Init_L(LNode* L,int n)
{
    int i;
    LNode *q;
    q = L;
    for(i = 1; i <= n;i++)//尾插法
    {
        LNode *p = (LNode*)malloc(sizeof(LNode));
        if(!p)
        {
            return ERROR;
        }
        p->data = i;
        p->next = NULL;
        q->next = p;
        p->prior = q;
        q = q->next; 
    }
    q->next = L;//循环
    L->prior = q;

    return OK;  

}
void Output(LNode *L)
{
    LNode *p;
    p = L->next;
    while(p != L)
    {
        printf("%4d",p->data);
        p = p->next;
    }
    printf("\n");

}

int main()
{
    LNode *L = (LNode*)malloc(sizeof(LNode));
    L->next = L;
    L->prior = L;
    LNode *p = NULL;
    LNode *q = NULL;
    int n;
    int count = 0;
    printf("please input the number of people:");
    scanf("%d",&n);

    Init_L(L,n);
    p = L->next;
    Output(L);

    while(L->next->next != L)
    {
        count++;
        if(p == L)
        {
            count--;
        }
        if(count == 3)
        {
            count = 0;
            p->prior->next = p->next;
            p->next->prior = p->prior;
            q = p;
            p = p->next;
            free(q);
        }
        else
        {
            p = p->next;
        }
        
        
    }
    printf("the last one is %d\n",L->next->data);

    return 0;

}

7、 题目:创建两个学生链表,含有姓名、年龄的信息,一个链表存放男生,一个链表存放女生,将上面两个链表合并,按学生的年龄进行排序,合成新的链表,将上题中建立的链表进行反转,实现按年龄的逆序排列

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

#define ERROR 0
#define OK 1

typedef struct LNode
{
    char name[20];
    int age;
    struct LNode *next;
}LNode;

int main()
{
    int Init_L(LNode *L,char *name,int age);
    void Output(LNode *L);
    void Merge_L(LNode *L1,LNode *L2,LNode *L);
    void Verse(LNode *L);

    LNode *ML = (LNode*)malloc(sizeof(LNode));
    LNode *WL = (LNode*)malloc(sizeof(LNode));
    LNode *L = (LNode*)malloc(sizeof(LNode));
    ML->next = NULL;
    WL->next = NULL;
    L->next = NULL;

    int age;
    char a[20];
    char ch;
    while(1)
    {
        printf("input input the choice(m is man ,w is woman,q is quit):");
        scanf("%c",&ch);
        getchar();
        if(ch == 'q')
        {
            break;
        }
        printf("please input the imformation:");
        scanf("%s %d",a,&age);
        getchar();
        if(ch == 'm')
        {
            Init_L(ML,a,age);
        }
        else if(ch == 'w')
        {
            Init_L(WL,a,age);
        }
        
    }

    Output(ML);
    Output(WL);

    Merge_L(ML,WL,L);//合并按照年龄顺序从小到大把
    Output(L);
    //头插法反转,比较简单 ,这里写的是就地翻转
    Verse(L);
    Output(L);

    free(ML);
    free(WL);
    free(L);
    return 0;

}

//构造链表
int Init_L(LNode *L,char *name,int age)//头插法
{
    LNode *p = (LNode *)malloc(sizeof(LNode));
    if(!p)
    {
        return ERROR;
    }
    p->age = age;
    strcpy(p->name,name);

    p->next = L->next;
    L->next = p;
    return OK;

}

//输出
 void Output(LNode *L)
 {
    LNode *p;
    p = L->next;
    while(p)
    {
        printf("%15s  %5d\n",p->name,p->age);
        p = p->next;
    }
    printf("\n");
 }

//判断插入
int Insert_L(LNode *L,LNode *s)
 {
    LNode *p;
    if(!L->next)
    {
        L->next = s;
        s->next = NULL;
        return OK;
    }
    p = L;
    while(p->next&&s->age > p->next->age)
    {
        p = p->next;
    }
    if(!p->next)
    {
        p->next = s;
        s->next = NULL;
    }
    else
    {
        s->next = p->next;
        p->next = s;
    }

    return OK;
    
 }

//有序合并
 void Merge_L(LNode *L1,LNode *L2,LNode *L)
 {  
    int min;
    LNode *p1;
    LNode *p2;
    LNode *p;
    p1 = L1->next;
    p2 = L2->next;

    p = p1;
    while(p1)
    {
        p = p1;
        p1 = p1->next;
        Insert_L(L,p);  
    }
    p = p2;
    while(p2)
    {
        p = p2;
        p2 = p2->next;
        Insert_L(L,p);   
    }
   
 }

//就地反转
void Verse(LNode *L)
{
    LNode *p;
    LNode *q;
    p = L->next;
    while(p->next)
    {
        q = p->next;
        p->next = q->next;
        q->next = L->next;
        L->next = q;
    }
}

8、在上面的实现的新链表中,给定一个学生的年龄,迅速查找和该学生年龄最接近的学生姓名和当前年龄差最小使用双向链表

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

#define ERROR 0
#define OK 1

typedef struct LNode
{
    char name[20];
    int age;
    struct LNode *next;
    struct LNode *prior;
}LNode;

int main()
{
    int Init_L(LNode *L,char *name,int age);
    void Output(LNode *L);
    void Merge_L(LNode *L1,LNode *L2,LNode *L);
    void Verse(LNode *L);

    LNode *ML = (LNode*)malloc(sizeof(LNode));
    LNode *WL = (LNode*)malloc(sizeof(LNode));
    LNode *L = (LNode*)malloc(sizeof(LNode));
    ML->next = NULL;
    ML->prior = NULL;
    WL->next = NULL;
    WL->prior = NULL;
    L->next = NULL;
    L->next = NULL;

    int age;
    char a[20];
    char ch;
    int x;
    LNode *p;
    while(1)
    {
        printf("input input the choice(m is man ,w is woman,q is quit):");
        scanf("%c",&ch);
        getchar();
        if(ch == 'q')
        {
            break;
        }
        printf("please input the imformation:");
        scanf("%s %d",a,&age);
        getchar();
        if(ch == 'm')
        {
            Init_L(ML,a,age);
        }
        else if(ch == 'w')
        {
            Init_L(WL,a,age);
        }
        
    }

    Output(ML);
    Output(WL);

    Merge_L(ML,WL,L);//合并按照年龄顺序从小到大把
    Output(L);
    //头插法反转,比较简单 ,这里写的是就地翻转
    Verse(L);
    Output(L);

    printf("please input the age you want find:");
    scanf("%d",&x);
    p = L->next;

    while(p)
    {
        if(x == p->age)
        {
            break;
        }
        p = p->next;
    }
    if(p->next != NULL&&p->prior != NULL)
    {
        if((p->next->age-x)>(x-p->prior->age))
        {
            printf("%s\n",p->next->name);
        }
        else
        {
            printf("%s\n",p->prior->name);
        }
    }
    else if(p->next==NULL)
    {
        printf("%s\n",p->prior->name);
    }
    else
    {
        printf("%s\n",p->next->name);
    }
    
    free(ML);
    free(WL);
    free(L);
    return 0;

}

//构造链表
int Init_L(LNode *L,char *name,int age)//头插法
{
    LNode *p = (LNode *)malloc(sizeof(LNode));
    if(!p)
    {
        return ERROR;
    }
    p->age = age;
    strcpy(p->name,name);

    p->next = L->next;
    if(L->next != NULL)
    {
        L->next->prior = p;
    }
    L->next = p;
    p->prior = L;
    return OK;

}

//输出
 void Output(LNode *L)
 {
    LNode *p;
    p = L->next;
    while(p)
    {
        printf("%15s  %5d\n",p->name,p->age);
        p = p->next;
    }
    printf("\n");
 }

//判断插入
int Insert_L(LNode *L,LNode *s)
 {
    LNode *p;
    if(!L->next)
    {
        L->next = s;
        s->prior = L;
        s->next = NULL;
        return OK;
    }
    p = L;
    while(p->next&&s->age > p->next->age)
    {
        p = p->next;
    }
    if(!p->next)
    {
        p->next = s;
        s->next = NULL;
        s->prior = p;
    }
    else
    {
        s->next = p->next;
        p->next->prior = s;
        p->next = s;
        s->prior = p;
    }

    return OK;
    
 }

//有序合并
 void Merge_L(LNode *L1,LNode *L2,LNode *L)
 {  
    int min;
    LNode *p1;
    LNode *p2;
    LNode *p;
    p1 = L1->next;
    p2 = L2->next;

    p = p1;
    while(p1)
    {
        p = p1;
        p1 = p1->next;
        Insert_L(L,p);  
    }
    p = p2;
    while(p2)
    {
        p = p2;
        p2 = p2->next;
        Insert_L(L,p);   
    }
   
 }

//就地反转
void Verse(LNode *L)
{
    LNode *p;
    LNode *q;
    p = L->next;
    while(p->next)
    {
        q = p->next;
        p->next = q->next;
        if(q->next != NULL)
        {
            q->next->prior = p;
        }
        q->next = L->next;
        L->next->prior = q;
        L->next = q;
        q->prior = L;
    }
}

9、利用链表实现一个先入后出的栈结构,并提供栈操作的pushpop的接口头插法最后进最先出

#include<stdio.h>//头插法先进先出
#include<stdlib.h>//晚上改个标准的

#define Elemtype int

typedef struct SNode
{
    Elemtype data;
    struct SNode *next;
}SNode;

void push(SNode *L,Elemtype e)
{
    SNode *p = (SNode *)malloc(sizeof(SNode));
    p->data = e;
    p->next = L->next;
    L->next = p;
}

void pop(SNode *L,Elemtype *e)
{
    SNode *q;
    if(!L->next)
    {
        exit(0);
    }
    q = L->next;
    *e = q->data;
    L->next = q->next;
    free(q);
}

int main()
{
    SNode *L = (SNode *)malloc(sizeof(SNode));
    SNode *p = NULL;
    L->next = NULL;
    int i;
    for(i = 0;i < 5;i++)
    {
        push(L,i+1);
    }
    p = L->next;
    while(p)
    {
        printf("%4d",p->data);
        p = p->next;
    }
    printf("\n");
    
    while(L->next)
    {
        pop(L,&i);
        printf("%4d",i);
    }
    printf("\n");

    return 0;
}