文章标题

来源:互联网 发布:秋瓷炫长相知乎 编辑:程序博客网 时间:2024/06/13 11:32

华为2016校园招聘上机笔试题

这是我第一次写博客,我希望把这个习惯延续下去

1. 题目
求取多个学生成绩中最好的成绩,通过指令以显示最高分或者更新某个学生的成绩

  • 输入描述 :
    开始输入两个正整数a,b,其中a表示学生数,b表示操作的次数。然后依次输入所有学生的成绩,学生学号默认正整数且从1依次累加。接下来b行以(char p,int first,int second )形式输入操作,p为Q时,查询first到second中最好的成绩;p为U时,将第first个学生的成绩更新为second。
  • 输出描述
    输出每次查询的最好的成绩
  • 输入内容
    5 5
    98 91 68 45 21
    q 1 5
    u 2 100
    q 1 4
    u 2 60
    q 1 5

  • 输出内容
    98
    100
    98

  • code
#include<iostream>using namespace std;const int MAXSIZE =30000;    // 学生数上限const int MAXOPETIME = 5000; // 操作数上限int Grade[MAXSIZE];int GetBestGrade(int firstnum,int secondnum);void UpdateGrade(int stunum,int grade);void error();void main(){    int cntofstu = 0;    int opetime =0 ;     int flag = 0 ;    char operation;    int i = 0;    int firstnum,secondnum;    cout<<"输入学生数以及操作次数"<<endl;    cin>>cntofstu>>opetime;    cout<<"依序输入学生成绩"<<endl;    while(i < cntofstu)    {        cin>>Grade[i];        i++;    }    cout<<"输入操作"<<endl;    Input:    cin>>operation>>firstnum>>secondnum;    switch (operation)    {        case 'q':case'Q': cout<<GetBestGrade(firstnum,secondnum)<<endl;break;        case 'u':case'U':UpdateGrade(firstnum,secondnum);break;    default:        cout<<"Wrong Operation,Input again"<<endl;        goto Input;        break;    }    flag++;    while(flag<opetime)    {        goto Input;    }    getchar();}/*返回最好成绩*/int GetBestGrade(int firstnum,int secondnum){    int i = firstnum - 1;    int bestgrade = Grade[i];    while(i <= secondnum-1)    {        if(Grade[i] > bestgrade)        {            bestgrade = Grade[i];        }        i++;    }    return bestgrade;}/*更新成绩*/void UpdateGrade(int stunum,int grade){    Grade[stunum - 1] =grade;}

这是我第一次写博客,然后代码也有很多bug。不过迈出第一步是值得鼓励的,希望我今后能写出好的代码。

0 0
原创粉丝点击