查找该学号学生的成绩。

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

你可能感兴趣的:(c语言)