sse哈工大C语言编程练习44

2026 年 3 月 16 日

收获:

1.当一个对象有多个属性时,可考虑使用结构体完成输入和查找。


1. 奇偶排序(Q6410)

题目描述:

输入 10 个数,将 10 个整数按升序排列输出,并且奇数在前,偶数在后。可利用 2 个数组变量,一个用来存放输入的整数,输入后,对这个数组进行排序,然后将数据复制到另一个数组中,先复制奇数再复制偶数。

输入提示: "Input 10 numbers:"
输入格式: "%d"
输出格式: "Output: %d,%d,%d,%d,%d,%d,%d,%d,%d,%d"

示例:

复制代码
Input 10 numbers:23 1 23 34 56 25 54 45 23 13
Output: 1,13,23,23,23,25,45,34,54,56↙
c 复制代码
#include <stdio.h>

int main()
{
    int a[10], result[10];
    
    printf("Input 10 numbers:");
    // 输入 10 个数字
    for(int i = 0; i < 10; i++){
        scanf("%d", &a[i]);
    }
    
    // 冒泡排序:将数组 a 按升序排列
    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 10 - i - 1; j++){
            if(a[j] > a[j + 1]){
                int tmp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tmp;
            }
        }
    }
    
    // 先将奇数放入 result 数组
    int front = 0;
    for(int i = 0; i < 10; i++){
        if(a[i] % 2 == 1) result[front++] = a[i];
    }
    
    // 再将偶数放入 result 数组
    for(int i = 0; i < 10; i++){
        if(a[i] % 2 == 0) result[front++] = a[i];
    }
    
    // 输出结果
    printf("Output: %d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
           result[0], result[1], result[2], result[3], result[4],
           result[5], result[6], result[7], result[8], result[9]);
    return 0;
}

2. 判断整数的正负性和奇偶性(Q1202)

题目描述:

编程判断输入整数的正负性和奇偶性。

输入格式要求: "%d" 提示信息:"Input m:"
输出格式要求:

  • 如果是负偶数,则输出"%d is a negative even\n"
  • 如果是负奇数,则输出"%d is a negative odd\n"
  • 如果是正偶数,则输出"%d is a positive even\n"
  • 如果是正奇数,则输出"%d is a positive odd\n"
  • 如果是 0,则输出"%d is zero. It is an even\n"

程序运行示例 1:

复制代码
Input m:6↙
6 is a positive even

程序运行示例 2:

复制代码
Input m:0↙
0 is zero. It is an even
c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m;
    
    printf("Input m:");
    scanf("%d", &m);
    
    if(m == 0){
        // 0 的情况
        printf("%d is zero. It is an even\n", m);
    } else if(m < 0){
        // 负数情况
        if(abs(m) % 2 == 1){
            printf("%d is a negative odd\n", m);
        } else {
            printf("%d is a negative even\n", m);
        }
    } else {
        // 正数情况
        if(m % 2 == 1){
            printf("%d is a positive odd\n", m);
        } else {
            printf("%d is a positive even\n", m);
        }
    }
    return 0;
}

3. 字符串逆序(字符数组)(Q108)

题目描述:

用字符数组作函数参数编程,利用一个数组实现字符串(允许输入带空格的字符串)的逆序存放。

要求如下:

  1. 在主函数中从键盘输入字符串,字符串的最大长度为 80 个字符,调用 Inverse() 函数将字符串逆序存放,然后在主函数中输出逆序后的字符串。
  2. 在子函数 Inverse() 中实现字符串的逆序存放。函数原型为:void Inverse(char str[]);
  3. 输入提示信息: "Input a string:\n"
    输出提示信息: "Inversed results:\n"
    用 gets() 输入字符串,用 puts() 输出字符串

注:

  1. 不能使用指针、结构体、共用体、文件、goto、枚举类型进行编程。
  2. 用纯 C 语言编程,所有变量必须在第一条可执行语句前定义。
c 复制代码
#include <stdio.h>
#include <string.h>

void Inverse(char str[]);

int main()
{
    char str[80];
    
    printf("Input a string:\n");
    gets(str);  // 输入字符串(可含空格)
    
    Inverse(str);  // 调用逆序函数
    
    printf("Inversed results:\n");
    puts(str);  // 输出逆序后的字符串
    return 0;
}

// 字符串逆序函数
void Inverse(char str[]){
    int len = strlen(str);
    char temp;
    
    // 双指针法:首尾交换
    for(int i = 0; i < len / 2; i++){
        temp = str[i];
        str[i] = str[len - 1 - i];
        str[len - 1 - i] = temp;
    }
}

4. 小写字母转大写字母(Q6443)

题目描述:

编程:将小写字母转换为大写字母。要求输入用 getchar();输出用 printf 函数。

程序运行结果示例:

复制代码
输入一个小写字符
a
Converted character: A,65

输入提示: "Enter a lowercase character\n"
输入格式: getchar()
输出格式: "Converted character: %c,%d\n"

c 复制代码
#include <stdio.h>

int main()
{
    char c;
    
    printf("输入一个小写字符\n");
    c = getchar();  // 读取一个字符
    
    // 小写转大写:减去'a'与'A'的 ASCII 码差值
    c = c - 'a' + 'A';
    
    printf("转换以后的字符为:%c,%d\n", c, c);
    return 0;
}

说明:

  • 小写字母'a'的 ASCII 码是 97
  • 大写字母'A'的 ASCII 码是 65
  • 转换公式:大写 = 小写 - 'a' + 'A'大写 = 小写 - 32

5. 学生成绩查找(结构体)(Q543)

题目描述:

从键盘输入某班学生某门课的成绩(每班人数最多不超过 40 人),当输入为负值时,表示输入结束,试编程从键盘任意输入一个学号,查找该学号学生的成绩。

输入格式要求: "%ld"(学号) "%ld%d" 提示信息:"Total students are %d\n" "Input the searching ID:" "Input student's ID and score:"
输出格式要求: "score = %d\n" "Not found!\n"

程序的两次运行示例:

示例 1:

复制代码
Input student's ID and score:070310122 84
Input student's ID and score:070310123 83
Input student's ID and score:070310124 88
Input student's ID and score:070310125 87
Input student's ID and score:070310126 61
Input student's ID and score:-1 -1
Total students are 5
Input the searching ID:070310123
score =  83

示例 2:

复制代码
Input student's ID and score:070310122 84
Input student's ID and score:070310123 83
Input student's ID and score:070310124 88
Input student's ID and score:070310125 87
Input student's ID and score:070310126 61
Input student's ID and score:-1 -1
Total students are 5
Input the searching ID:070310128
Not found!
c 复制代码
#include <stdio.h>

// 定义学生结构体
struct student{
    long xh;      // 学号
    int score;    // 成绩
};

int main()
{
    int n = 0, flag = 0;
    long ID;
    struct student S[40];  // 最多 40 个学生
    
    // 循环输入学生信息,直到输入负值
    while(1){
        printf("Input student's ID and score:");
        scanf("%ld %d", &S[n].xh, &S[n].score);
        
        // 如果学号或成绩为负值,结束输入
        if(S[n].xh < 0 || S[n].score < 0){
            break;
        }
        n++;
    }
    
    printf("Total students are %d\n", n);
    printf("Input the searching ID:");
    scanf("%ld", &ID);
    
    // 查找指定学号的学生
    for(int i = 0; i < n; i++){
        if(S[i].xh == ID){
            flag = 1;
            printf("score = %d\n", S[i].score);
        }
    }
    
    // 如果没有找到
    if(flag == 0){
        printf("Not found!");
    }
    return 0;
}
相关推荐
特种加菲猫2 小时前
透过源码看本质:list 的模拟实现与核心原理
开发语言·c++
李日灐2 小时前
改造红黑树实现封装 map/set:感受C++ 标准容器的精妙设计与底层实现
开发语言·数据结构·c++·后端·算法·红黑树
李日灐2 小时前
【优选算法1】双指针经典算法题
数据结构·c++·后端·算法·刷题·双指针
Frostnova丶2 小时前
(9)LeetCode 438.找到字符串中所有字母异位词
算法·leetcode
故事和你912 小时前
sdut-程序设计基础Ⅰ-期末测试(重现)
大数据·开发语言·数据结构·c++·算法·蓝桥杯·图论
Ralph_Y2 小时前
C++ 常量的定义方式与内存存储位置全解析
c++
努力学算法的蒟蒻2 小时前
day114(3.16)——leetcode面试经典150
算法·leetcode·职场和发展
重庆兔巴哥2 小时前
如何在Dev-C++中使用MinGW-w64编译器?
linux·开发语言·c++
ysa0510302 小时前
贪心【逆向dp】
数据结构·c++·笔记·算法