C语言刷题系列——14.(结构)计算两个复数之积15.按等级统计学生成绩16.根据成绩高低将学生记录排序

14.计算两个复数之积

🐳题目要求

要求实现一个计算复数之积的简单函数

函数接口定义:
struct complex multiply(struct complex x, struct complex y);

struct complex是复数结构体,其定义如下:

struct complex
{
    int real;
    int imag;
};
裁判测试程序样例:
#include <stdio.h>

struct complex{
    int real;
    int imag;
};

struct complex multiply(struct complex x, struct complex y);

int main()
{
    struct complex product, x, y;

    scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
    product = multiply(x, y);
    printf("(%d+%di) * (%d+%di) = %d + %din", 
            x.real, x.imag, y.real, y.imag, product.real, product.imag);
    
    return 0;
}

/* 你的代码将被嵌在这里 */

🐳题解

🌨step1:先看这个函数的返回类型:struct complex

因此,函数内部声明一个结构体变量

 struct complex co;

🌨step2:根据复数相乘的规则,计算real、imag

real——实部 = 两数的实部的乘积 - 两数虚部的乘积(因为i方 = -1)

imag——虚部 = 两个实部*虚部,相加

🌨step3:完整代码:

struct complex multiply(struct complex x, struct complex y)
{
    struct complex co;
    co.real = x.real * y.real - x.imag * y.imag;
    co.imag = x.real * y.imag + x.imag * y.real;
    
    return co;
}

15.按等级统计学生成绩

🐤题目要求

要求实现一个根据学生成绩设置其等级,并统计不及格人数的简单函数

函数接口定义:
int set_grade( struct student *p, int n );

其中p是指向学生信息的结构体数组的指针,该结构体的定义为:

struct student
{
    int num;
    char name[20];
    int score;
    char grade;
};

n是数组元素个数。学号num、姓名name和成绩score均是已经存储好的。set_grade函数需要根据学生的成绩score设置其等级grade。等级设置:85-100为A,70-84为B,60-69为C,0-59为D。同时,set_grade还需要返回不及格的人数。

裁判测试程序样例:
#include <stdio.h>
#define MAXN 10

struct student{
    int num;
    char name[20];
    int score;
    char grade;
};

int set_grade( struct student *p, int n );

int main()
{   struct student stu[MAXN], *ptr;
    int n, i, count;
    
    ptr = stu;
    scanf("%dn", &n);
    for(i = 0; i < n; i++){
       scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
    } 
   count = set_grade(ptr, n);
   printf("The count for failed (<60): %dn", count);
   printf("The grades:n"); 
   for(i = 0; i < n; i++)
       printf("%d %s %cn", stu[i].num, stu[i].name, stu[i].grade);
    return 0;
}

/* 你的代码将被嵌在这里 */

🐤题解

🏀step1

根据各个等级满足的条件,令p[i].grade 等于相应的ABCD

🏀step2

返回不合格(D等级)的人数:每个满足D的if中n++即可

🏀step3:完整代码

int set_grade( struct student *p, int n )
{
    int i = 0;
    int n = 0;
    for(i = 0; i<n; i++)
    {
        if(p[i].score >=85)
            p[i].grade = 'A';
        else if(p[i].score >=70)
            p[i].grade = 'B';
        else if(p[i].score >=60)
            p[i].grade = 'C';
        else if(p[i].score <60)
        {
            p[i].grade = 'D';
            n++;
        }
    } 
    return n;
}

16.结构体数组排序-根据成绩高低将学生记录排序

⌛题目要求

定义函数处理结构体数组,按成绩从高到低降序排列

函数接口定义:
struct stu {
    int num;
    char name[20];
    int score;  };

void  fun ( struct stu *p, int n );

p是结构体数组的起始地址,n是结构体单元个数。

结构体成员:num是学号,name是姓名,score是成绩。

裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include <stdio.h>
struct stu{
    int num;
    char name[20];
    int score;
};
void fun( struct stu *p, int n );
int main()
{   struct stu a[10];
    int n, i;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
       scanf("%d%s%d", &a[i].num, a[i].name, &a[i].score);
    } 
   fun(a,n);
   for(i = 0; i < n; i++)
       printf("%d %s %dn", a[i].num, a[i].name, a[i].score);
    return 0;
}
/* 请在这里填写答案 */

⌛题解:选择排序

大致思路:结构体数组从左到右依次遍历(从下标i = 0到i = n - 1),找到最大值对应数组的下标,存储到变量max中,如果max == i,说明最大值对应的下标就是i,不用再处理;如果max != i , 说明最大元素的下标不是i,因此要把这俩结构体变量交换

void  fun ( struct stu *p, int n )
{
    int i,j;
    int max = 0; 
    for(i = 0;i<n-1;i++)
    {
        max = i;
        for(j = i+1;j<n;j++)
        {
            if(p[j].score>p[max].score)
                max = j;
        }

        if(i!=max)
        {
            struct stu tem = p[i];
            p[i] = p[max];
            p[max] = tem;
        }
    }
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>