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

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 ;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值