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 i → int i; (缺少分号)
  3. int i → int i = 0; (未初始化)
  4. scanf("%d%d", num[i], income[i]) → scanf("%ld %d", &num[i], &income[i]) (缺少&和格式错误)
  5. num[i] >0 || score[i] >= 0 → num[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;  // 返回平均值
}
相关推荐
Sarvartha4 小时前
final 关键字
java·开发语言
yi0114 小时前
LeetCode 219:存在重复元素 II——哈希表记录“最近一次出现的位置”
数据结构·人工智能·笔记·python·算法·leetcode·哈希表
程序员Sunday4 小时前
JavaScript 事件循环面试题,宏任务与微任务怎么执行|Sunday面试指南
开发语言·javascript·面试·校招·事件循环·程序员sunday
zhangzeyuaaa5 小时前
Ruby 多线程、GVL(GIL)与 Mutex 完全指南
开发语言·前端·ruby
谢亮_vipxieliang6 小时前
用 PHP 构建轻量级 REST API:从路由到鉴权的完整实践
开发语言·后端·php
Escalating_xu6 小时前
【C 语言】数据在内存中的存储:补码、大小端、整型陷阱与 IEEE 754 全解析
java·c语言·网络
思无邪666 小时前
用 AI 做 JS 逆向:从抓包到复现的完整方法论
开发语言·javascript·人工智能
灯澜忆梦6 小时前
【面向对象编程C++】| 基础语法
java·c++·算法
小蒋观天下7 小时前
专项方案:大场景港口AI安防、多干扰环境下的算法调优与落地实操
人工智能·深度学习·算法·安全·机器学习·计算机视觉·ai大模型
Evand J7 小时前
【电机滤波例程5】渐消因子自适应扩展卡尔曼滤波(EKF),MATLAB。PMSM负载突变下的状态估计。附下载链接
算法·matlab