2019年成都信息工程大学807大题代码

求二维数组周边元素之和

  #include "stdio.h"
#define M 4
#define N 5
int fun(int Matrix[][N])
{
    int sum = 0;
    for (int i = 0; i < M; i++)
        if (i != 0 && i != M - 1)
            sum += Matrix[i][0] + Matrix[i][N - 1];
        else
            for (int j = 0; j < N; j++)
                sum += Matrix[i][j];
    return sum;
}
int main()
{
    int num;
    int Matrix[M][N] = {0};
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
        {
            scanf("%d", &Matrix[i][j]);
        }
    num = fun(Matrix);
    printf("%d", num);
}
// int Matrix[M][N] = {{1, 3, 5, 7, 9}, {2, 9, 9, 9, 4}, {6, 9, 9, 9, 8}, {1, 3, 5, 7, 0}};// 测试用例

学生成绩处理

#include "stdio.h"
#define N 5

int main()
{
    struct stu
    {
        char id[10];
        int math;
        int c_language;
    } stu[N];
    int c_language_max = 0;
    int sum;
    printf("请输入各位同学的信息\n");
    printf("学号 数学 C语言\n");
    for (int i = 0; i < N; i++)
    {
        scanf("%s %d %d", stu[i].id, &stu[i].math, &stu[i].c_language);
        if (stu[i].c_language > c_language_max)
        {
            c_language_max = stu[i].c_language;
            sum = 1;
        }
        else if (stu[i].c_language == c_language_max)
        {
            sum++;
        }
    }
    printf("C语言最高分:%d,人数:%d\n", c_language_max, sum);
    printf("C语言分数为%d的同学相关信息如下:\n", c_language_max);
    printf("学号 数学 C语言\n");
    for (int i = 0; i < N; i++)
    {
        if (stu[i].c_language == c_language_max)
            printf("%s %d %d\n", stu[i].id, stu[i].math, stu[i].c_language);
    }
}

字符串排序

#include "stdio.h"
#include "string.h"
#define N 10
void sort(char strs[][21], int type)
{
    char s[21];

    for (int i = 0; i < N - 1; i++)
    {
        for (int j = N - 1; j > i; j--)
        {
            if (type == 1 && (strcmp(strs[j], strs[j - 1]) < 0))
            {
                strcpy(s, strs[j]);
                strcpy(strs[j], strs[j - 1]);
                strcpy(strs[j - 1], s);
            }
            else if (type == 0 && (strcmp(strs[j], strs[j - 1]) > 0))
            {
                strcpy(s, strs[j]);
                strcpy(strs[j], strs[j - 1]);
                strcpy(strs[j - 1], s);
            }
        }
    }
}

int main()
{
    char strs[N][21];
    int type;

    printf("请输入10个字符串\n");
    for (int i = 0; i < N; i++)
    {
        scanf("%s", strs[i]);
    }
    printf("请输入排序方式,按字母增序:1,按字母减序:0\n");
    printf("type =");
    scanf("%d", &type);

    sort(strs, type);

    for (int i = 0; i < N; i++)
    {
        printf("%s\n", strs[i]);
    }
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值