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

2026 年 3 月 14 日

收获:

1.统计数量时,若结束是通过输入判断的,最后一个数据不统计在有效数据之中。


1. 职工收入查找(改错)(Q3403)

题目描述:

从键盘输入某单位职工的月收入 (人数最多不超过 40 人),当输入负值时,表示输入结束,编程从键盘任意输入一个职工号,查找该职工的月收入。

程序运行示例 1:

复制代码
Input person's ID and income:001 1000↙
Input person's ID and income:002 4500↙
Input person's ID and income:003 500↙
Input person's ID and income:004 2100↙
Input person's ID and income:005 7000↙
Input person's ID and income:006 3000↙
Input person's ID and income:007 -1↙
Total number is 6
Input the searching ID:005↙
income = 7000

程序运行示例 2:

复制代码
Input person's ID and income:001 235↙
Input person's ID and income:002 365↙
Input person's ID and income:003 789↙
Input person's ID and income:004 512↙
Input person's ID and income:005 -5↙
Total number is 4
Input the searching ID:007↙
Not found!

原代码(有错误):

c 复制代码
#include <stdio.h>
#define N 40
int ReadScore(int income[], long num[]);         
int LinSearch(long num[], long x, int n);         
int main()
{
    int income[N], n, pos;
    long num[N], x;
    n = ReadScore(income, num);  
    printf("Total number is %d\n", n);
    printf("Input the searching ID:");
    scanf("%d", &x);              // 错误 1:应该用%ld 格式
    pos = LinSearch(num, x, n); 
    if (pos != -1)                  
    {  
        printf("income = %d\n", income[pos]); 
    }
    else                                
        {   
        printf("Not found!\n");     
    }
    return 0;
}

int ReadScore(int income[], long num[]) 
{
    int i           // 错误 2:缺少分号
    do{
        i++;        // 错误 3:i 未初始化就自增
        printf("Input person's ID and income:");
        scanf("%d%d", num[i], income[i]);  // 错误 4:缺少&取地址符
    } while (num[i] >0 || score[i] >= 0);  // 错误 5:变量名错误,逻辑运算符错误
    return i;                                
}

int LinSearch(long num[], long x, int n) 
{
    int  i;  
    for (i=0; i<=n; i++)                  // 错误 6:循环条件错误
    {   
        if (num[i] = x)  return (i);      // 错误 7:应该用==比较
    }
    return (-1);                           
}

改正后的代码:

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

int ReadScore(int income[], long num[]);         
int LinSearch(long num[], long x, int n);         

int main()
{
    int income[N], n, pos;
    long num[N], x;
    
    n = ReadScore(income, num);  
    printf("Total number is %d\n", n - 1);  // 修正 1:减去最后无效的计数
    printf("Input the searching ID:");
    scanf("%ld", &x);              // 修正 2:使用%ld 格式
    
    pos = LinSearch(num, x, n); 
    
    if (pos != -1){  
        printf("income = %d\n", income[pos]); 
    } else {
        printf("Not found!\n");     
    }
    return 0;
}

int ReadScore(int income[], long num[]) 
{
    int i = 0;  // 修正 3:初始化 i
    do{
        i++;
        printf("Input person's ID and income:");
        scanf("%ld %d", &num[i], &income[i]);  // 修正 4:添加&取地址符,修正格式
    } while (num[i] > 0 && income[i] >= 0);  // 修正 5:使用&&且删除 score
    return i;                                
}

int LinSearch(long num[], long x, int n) 
{
    int  i;  
    for (i = 0; i < n; i++)  // 修正 6:i<n 而不是 i<=n
    {   
        if (num[i] == x) return (i);  // 修正 7:使用==比较
    }
    return (-1);                           
}

主要错误总结:

  1. scanf("%d", &x)scanf("%ld", &x) (long 类型用%ld)
  2. int iint i; (缺少分号)
  3. int iint i = 0; (未初始化)
  4. scanf("%d%d", num[i], income[i])scanf("%ld %d", &num[i], &income[i]) (缺少&和格式错误)
  5. num[i] >0 || score[i] >= 0num[i] > 0 && income[i] >= 0 (变量名和逻辑运算符错误)
  6. for (i=0; i<=n; i++)for (i = 0; i < n; i++) (避免越界)
  7. if (num[i] = x)if (num[i] == x) (应该用==比较)
  8. printf("Total number is %d\n", n)printf("Total number is %d\n", n - 1) (减去无效计数)

2. 统计奇数位数(Q4867)

题目描述:

输入任意一个非 0 整数,编程计算该整数每位上的数为奇数的个数。输入 0 时,表示结束程序。

输入提示信息: "Input an integer:\n"
输入格式: "%d"
输出提示信息和格式: "The number of even digits is %d\n"

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

int main()
{
    int n, count = 0;
    
    printf("Input an integer:");
    scanf("%d", &n);
    
    while(n){  // 输入 0 时结束
        count = 0;
        n = abs(n);  // 取绝对值
        
        // 统计每一位上奇数的个数
        while(n){
            if(n % 2 == 1) count++;  // 判断当前位是否为奇数
            n /= 10;  // 去掉最后一位
        }
        
        printf("The number of even digits is %d\n", count);
        printf("Input an integer:\n");
        scanf("%d", &n);
    }
    return 0;
}

3. 判断素数(Q210)

题目描述:

所谓素数是指这个数只能被 1 和自身整除。要求在主函数输入一个数,调用函数 Fun() 判断该数是否是素数。打印信息在主函数中进行。

输入数据提示信息: "Please input a number:\n"(该提示信息请放在循环体外)
输入数据格式为: "%d"
输出格式要求:

  • 若是素数输出数据格式为:"%d is a prime number\n"
  • 若不是素数输出数据格式为:"%d is not a prime number\n"
c 复制代码
#include <stdio.h>
#include <math.h>

int prime(int n);

int main()
{
    int n;
    
    printf("Please input a number:\n");
    scanf("%d", &n);
    
    // 检查输入合法性,小于 2 的数重新输入
    while(n < 2){
        scanf("%d", &n);
    }
    
    if(prime(n)){
        printf("%d is a prime number\n", n);
    } else {
        printf("%d is not a prime number\n", n);
    }
    return 0;
}

// 判断素数的函数
int prime(int n){
    if(n < 2) return 0;
    if(n == 2 || n == 3) return 1;
    if(n % 2 == 0 || n % 3 == 0) return 0;
    
    for(int i = 5; i <= sqrt(n); i += 6){
        if(n % i == 0 || n % (i + 2) == 0) return 0;
    }
    return 1;
}

4. 替换空格为下划线(指针实现)(Q2432)

题目描述:

给定如下的数组:

c 复制代码
char chars[] = { 'a', ' ', 'b', ' ', 'c', ' ', ' ', 'd' };

写一个程序将数组中所有的空格字符替换为下划线字符'_'。使用指针而不是下标访问数组元素。

输出格式要求: "替换后数组为:" "%c "

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

int main()
{
    char strs[2000] = {'a',' ','b',' ','c',' ',' ','d'}, *p = strs;
    
    // 使用指针遍历数组,替换空格
    while(*p != '\0'){
        if(*p == ' '){
            *p = '_';  // 将空格替换为下划线
        }
        p++;
    }
    
    // 输出结果
    printf("替换后数组为:");
    p = strs;  // 重置指针
    while(*p != '\0'){
        printf("%c ", *p);
        p++;
    }
    return 0;
}

5. 计算平均薪资(结构体)(Q6590)

题目描述:

假设有以下定义:

c 复制代码
#define N 5
struct Employee
{
    long id;
    char name[20];
    int salary;
};

请编写函数完成计算平均薪资。再在主函数调用这个函数。

输入提示: "please input id,name,salary:\n"(只提示一次)
输入格式: "%d%s%d"
输出格式: "average salary=%.2f\n"

程序运行示例:

复制代码
please input id,name,salary:↙
201 zhang 18000
202 li 10800
203 zhao 12000
204 wang 15000
205 sun 17000
average salary=14560.00↙
c 复制代码
#include <stdio.h>
#define N 5

struct Employee
{
    long id;
    char name[20];
    int salary;
};

// 函数声明:计算平均薪资
float cal(struct Employee e[]);

int main()
{
    struct Employee e[N];
    
    printf("please input id,name,salary:\n");
    // 输入员工信息
    for(int i = 0; i < N; i++){
        scanf("%ld %s %d", &e[i].id, e[i].name, &e[i].salary);
    }
    
    // 计算并输出平均薪资
    float avg = cal(e);
    printf("average salary=%.2f\n", avg);
    return 0;
}

// 计算平均薪资函数
float cal(struct Employee e[]){
    float sum = 0;
    
    // 累加所有员工的薪资
    for(int i = 0; i < N; i++){
        sum += e[i].salary;
    }
    
    return sum / N;  // 返回平均值
}
相关推荐
YYYing.1 小时前
【Linux/C++多线程篇(一) 】多线程编程入门:从核心概念到常用函数详解
linux·开发语言·c++·笔记·ubuntu
炸膛坦客2 小时前
单片机/C语言八股:(十三)C 语言实现矩阵乘法
c语言·开发语言·矩阵
一个有毅力的吃货2 小时前
这个SKILL打通了AI写公众号文章的最后一公里
css·算法
free-elcmacom2 小时前
C++ 函数占位参数与重载详解:从基础到避坑
java·前端·算法
Frostnova丶2 小时前
LeetCode 1415. 长度为 n 的开心字符串中字典序第 k 小的字符串
数据结构·算法·leetcode
美好的事情能不能发生在我身上2 小时前
Leetcode热题100中的:技巧专题
算法·leetcode·职场和发展
荣光属于凯撒2 小时前
P15755 [JAG 2025 Summer Camp #1] JAG Box
c++·算法·贪心算法
为你奋斗!2 小时前
Playwright 录屏功能启用离线安装依赖pywin32 pillow
开发语言·chrome·python·语言模型·迁移学习
郝学胜-神的一滴2 小时前
CMake:解锁C++跨平台工程构建的核心密钥
开发语言·c++·职场和发展