ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

NEFU高级程序设计-期末复习习题组

2021-10-27 18:33:24  阅读:200  来源: 互联网

标签:prelist char 复习 int void NEFU 习题 include cur


1. 用链表实现单词序列倒序输出

题目

用链表实现单词序列倒序输出。与以往不同,请考虑采用一种完全的动态分配方式! 为降低难度,“仁慈”的我已经给出了输出和释放的代码,你只要写出创建链表的creat函数定义就可以了。

比如输入为: abc bcd cde

则输出为: cde bcd abc

见题干!

你只能在代码输入框中:"//start(或#start)“行的下面,”//end(或#end)“行的上面输入你的代码, 而不能改变”//start(或#start)“以及其上所有行的代码,包括添加空格与空行, 也不能改变”//end(或#end)"以及其下所有行的代码,包括添加空格与空行.

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
struct node
{
    char * data;
    struct node *next;
};
typedef struct node NODE;
NODE* creat()
{
    NODE *head,*cur;
    char in[N];
    //start
    head=(NODE *)malloc(sizeof(NODE));
    head->next=NULL;
    while(scanf("%s", in)!=EOF)
    {
        cur=(NODE *)malloc(sizeof(NODE));
        cur->data=(char *)malloc(sizeof(char) * N);
        strcpy(cur->data, in);
        cur->next=head->next;
        head->next=cur;
    }
    //end
    return head;
}
int main()
{
    NODE *head,*cur,*pre;
    /*creat list*/
    head = creat();
    /*print list*/
    cur = head->next;
    while(cur!=NULL&&cur->next!=NULL)
    {
        printf("%s ",cur->data);
        cur = cur->next;
    }
    if(cur!=NULL)
        printf("%s\n",cur->data);
    /*release list*/
    while(head->next!=NULL)
    {
        pre = head;
        cur = head->next;
        while(cur->next!=NULL)
        {
            pre = cur;
            cur = cur->next;
        }
        free(cur->data);
        free(cur);
        pre->next = NULL;
    }
    return 0;
}

2. 二值图像处理

题目

别紧张,你只把这个简单的二值图像处理的问题当成C语言的一道题目就可以。 输入为一个nn的二维整型矩阵(矩阵中元素的最小值为0,最大值不超过1),输出其中0、1这两种值的个数。输入数 据有多组,首先输入一个整数n(0<n<10),代表矩阵的维数,接着是nn个0或者1。

比如输入为:

8

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 0

0 0 0 0 0 1 0 0

0 0 0 0 1 0 0 0

0 0 0 1 0 0 0 0

0 0 1 0 0 0 0 0

0 1 0 0 0 0 0 0

1 0 0 0 0 0 0 0

则输出为: 53 11

代码

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

int main()
{
    int n, ans0=0, ans1=0;
    scanf("%d", &n);
    for(int i=0;i<n*n;i++)
    {
        int tmp;
        scanf("%d", &tmp);
        if(!tmp)    ans0++;
        else    ans1++;
    }
    printf("%d %d", ans0, ans1);
    return 0;
}

3.计算不同类型字符的数量

题目

如标题所示。数据数据有多组,每组为一个用空白符分隔的字符串(长度<100),请试着统计其中字母(大小写均为字 母)、数字(0~9)和其它字符的个数并输出。

比如输入为: Iloveprograming1@2#3

则输出为: 15 3 2

注意:字符串可能以’\0’或者"\r\0"作为结束标志!

见题干!

代码

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

int main()
{
    char str[110];
    while(scanf("%s", str)!=EOF)
    {
        int ansalpha=0, ansnum=0, ansother=0;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            if(isalpha(str[i])) ansalpha++;
            else if(isalnum(str[i]))    ansnum++;
            else    ansother++;
        }
        printf("%d %d %d\n", ansalpha, ansnum, ansother);
    }
    return 0;
}

PS

这道题题目里面还给了结束符的类型,应该还要做具体的判定,在这里就先不用了,到时候具体问题还需要根据调试做修改

4. 实现字符串排序

题目

实现字符串排序。与以往不同,请考虑采用一种完全的动态分配方式。请你: 1. 定义一个整型变量n,用于确定数组字符串的个数; 2. 用动态分配定义一个指针数组,用于存储每一个待排序字符串的首地址; 3. 定义一个输入缓冲区(长度不超过100的一维字符数组),用于存储不带分隔符的字符串; 4. 根据输入缓冲区中当前串的长度,动态分配空间,并将其复制到新分配的空间中; 5. 用函数实现字符串按字典升序排序,并输出排序后的字符串(每串一行); 6. 记得释放所有之前动态分配开辟的空间!

比如输入为: 3 banana apple peach

则输出为: apple banana peach

代码

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

void sort(char **prelist, int n)
{
    char tmp[110];
    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            if(strcmp(prelist[i], prelist[j])>0)
            {
                strcpy(tmp, prelist[i]);
                strcpy(prelist[i], prelist[j]);
                strcpy(prelist[j], tmp);
            }
        }
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    char **prelist=(char **)malloc(sizeof(char *)*n);
    char str[110];
    for(int i=0;i<n;i++)
    {
        scanf("%s", str);
        int len=strlen(str);
        char *p=((char *)malloc(sizeof(char)*len));
        strcpy(p, str);
        prelist[i]=p;
    }
    sort(prelist, n);
    for(int i=0;i<n;i++)
    {
        printf("%s ", prelist[i]);
    }
    return 0;
}

代码(重构)

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

void sort(char **prelist, int n)
{
    char tmp[110];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(strcmp(prelist[i], prelist[j])<0)
            {
                strcpy(tmp, prelist[i]);
                strcpy(prelist[i], prelist[j]);
                strcpy(prelist[j], tmp);
            }
        }
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    char **prelist;
    prelist=(char **)malloc(sizeof(char *)*n);
    for(int i=0;i<n;i++)
    {
        char str[110];
        scanf("%s", str);
        char *newarr=(char *)malloc(sizeof(char) * strlen(str));
        strcpy(newarr, str);
        prelist[i]=newarr;
    }
    sort(prelist, n);
    for(int i=0;i<n;i++)
        printf("%s ", prelist[i]);
    return 0;
}

5. 战旗游戏

题目

我知道你们男生女生都喜欢打游戏,下面这道简单的编程题是某战棋游戏的一部分,实现的功能是向上、向下、向左、 向右、向左上、向左下、向右上、向右下这八个方向移动一格。请编写一个process函数,实现对上述八个函数的调 用。 比如:

输入2 3

则输出为:3 3

代码

#include <stdio.h>
#include <stdlib.h>
void process(int *x, int *y, void (*fun)(int *x, int *y))
{
    //start
    fun(x, y);
    //end
}
void up(int *x, int *y)
{
    *y = *y - 1;
    return ;
}
void down(int *x, int *y)
{
    *y = *y + 1;
    return ;
}
void left(int *x, int *y)
{
    *x = *x - 1;
    return ;
}
void right(int *x, int *y)
{
    *x = *x + 1;
    return ;
}
void up_left(int *x, int *y)
{
    *x = *x - 1;
    *y = *y - 1;
    return ;
}
void up_right(int *x, int *y)
{
    *x = *x + 1;
    *y = *y - 1;
    return ;
}
void down_left(int *x, int *y)
{
    *x = *x - 1;
    *y = *y + 1;
    return ;
}
void down_right(int *x, int *y)
{
    *x = *x + 1;
    *y = *y + 1;
    return ;
}
int main(void)
{
    int x,y;
    while(scanf("%d%d",&x,&y)==2)
    {
        process(&x,&y,up);
        process(&x,&y,right);
        process(&x,&y,right);
        process(&x,&y,up_right);
        process(&x,&y,left);
        process(&x,&y,down);
        process(&x,&y,down_left);
        process(&x,&y,up_left);
        process(&x,&y,down_right);
        printf("%d %d\n",x,y);
    }
    return 0;
}

6. 期末成绩统计

题目

期末大考周即将到来了。诸位要面临C语言、高数、大物的期末考试。请你编写一个程序,记录全班同学的三科成绩并 按C语言成绩的降序排序输出。 输入数据有多组:先用一个整型变量n存储班级同学的人数;接着存储这n名同学的三科(C语言、高数、大物)成绩 (类型为整型);排序后输出。 比如,

输入为: 3 张三 80 70 60 李四 90 80 70 王五 100 90 80

则输出为: 王五 100 90 80 李四 90 80 70 张三 80 70 60

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 20
#define N 40
struct stu
{
    char name[LEN];
    int scores[3];
}student[N];
int main(void)
{
    int n;
    void input(struct stu *s,int n);
    void sort(struct stu *s,int n);
    void print(struct stu *s, int n);
    while(scanf("%d",&n)==1)
    {
       input(student,n);
       sort(student,n);
       print(student,n);
    }
    return 0;
}
void input(struct stu *s,int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%s",s[i].name);
        for(j=0;j<3;j++)
           scanf("%d",&s[i].scores[j]);
    }
    return ;
}
void sort(struct stu *s,int n)
{
//start
    struct stu tmp;
    for(int i=0;i<n;i++)
        for(int j=i;j<n;j++)
        {
            if(s[i].scores[0]<s[j].scores[0])
            {
                tmp=s[i];
                s[i]=s[j];
                s[j]=tmp;
            }
        }
//end
}
void print(struct stu *s, int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("%s ", s[i].name);
        for(j=0;j<3;j++)
           if(j<2)
              printf("%d ",s[i].scores[j]);
           else
              printf("%d\n",s[i].scores[j]);
    }
    return ;
}

标签:prelist,char,复习,int,void,NEFU,习题,include,cur
来源: https://blog.csdn.net/Do1phln/article/details/120998911

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有