查找该学号学生的成绩。

该程序从键盘接收学号和对应的成绩,直到输入负值为止。然后,用户可以输入要查询的学号,程序会返回该学号对应的成绩,如果找不到则显示Notfound!。示例展示了两次运行情况,包括找到成绩和未找到成绩的情况。
摘要由CSDN通过智能技术生成

从键盘输入某班学生某门课的成绩(每班人数最多不超过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"

程序的两次运行示例如下:

① 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

② 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!

#include<stdio.h>
#define N 40
int main()
{
    int a[N], b[N], i, j, sum = 0, score;
    long int id;
    for (i = 0; i < N; i++)
    {
        printf("Input student’s ID and score:");
        scanf("%ld%d", &a[i], &b[i]);
        if (a[i] < 0 || b[i] < 0)break;
        sum++;
    }
    printf("Total students are %d\n" , sum);
    printf("Input the searching ID:");
    scanf("%ld", &j);
    i = 0;
    for (i = 0; i < N; i++)
    {
        if (a[i] == j && i < sum)
        {
            printf("score = %d\n", b[i]);
            break;
        }
        else if(i >= sum){
            printf("Not found!\n");break;
        } 
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值