zoukankan      html  css  js  c++  java
  • 成绩查询系统(链表实现)

    问题
    数学老师小y 想写一个成绩查询系统,包含如下指令:

    insert [name] [score],向系统中插入一条信息,表示名字为name的学生的数学成绩为score。
    find [name],表示查找名字为name的学生的数学成绩。
    注意有些同学可能会为了刷分多次选课,查询的时候给出最大成绩即可。学生的名字是由小写字母组成。成绩是一个 0 …100 的整数。

    老师找到你,想你帮他完成这个系统。

    输入格式

    输入若干行,每行都是insert [name] [score]或者find [name]的形式,或一行end表示输入结束。输入行数不大于 1000,每个学生名字长度不大于 20 个字符。

    输出格式

    对于每个查询,输出查询的学生的最高成绩,如果系统中不存在该学生,输出 -1。

    样例输入

    insert zhangsan 90
    insert lisi 78
    insert xiaoming 86
    find xiaoming
    find jack
    end

    样例输出

    86
    -1

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<stdio.h>
     4 #include<stdlib.h> 
     5 #include<string.h>
     6 #include<malloc.h>
     7 using namespace std;
     8 
     9 //定义链表结构
    10 typedef struct Node{ 
    11     char name[20]; //姓名 
    12     int score;     //分数 
    13     struct Node *next; //指针域 
    14 }Node,*List;
    15 
    16 //初始化,创建一个头结点,并使L指向此头结点 
    17 int Init_L(List &L){ 
    18     L = (List)malloc(sizeof(Node));
    19     if( !L )
    20         return 0;
    21     L->next = NULL;
    22     return 0;
    23 }
    24 
    25 bool complare(int a,int b){
    26      return a>b;
    27 }
    28 
    29 int main(){
    30     List L;
    31     Init_L(L);
    32     string operate;//记录所要进行的操作,insert、find 
    33     string name;//用于查找 
    34     List r,p;   //临时变量 
    35     r = L;      //保存头结点 
    36     while( cin>>operate ){
    37         if( operate == "end" )
    38             break ;
    39         else if( operate == "insert" ){
    40             char nam[20];
    41             int sco;
    42             p = (List)malloc(sizeof(Node));//创建临时结点 
    43             scanf("%s%d",nam,&sco);
    44             strcpy( p->name , nam );//将用户输入的姓名复制到p->name中,不能直接用 = 
    45             p->score = sco;//将用户输入的分数赋给p->score 
    46             r->next = p;   //将头结点的指针域指向新创建的结点p 
    47             r = p;         //r向后移动到p的位置(尾插法) 
    48             r->next = NULL;
    49         }
    50         else if( operate == "find" ){
    51             cin>>name; //用户输入要查找的姓名 
    52             List x;    //定义一个临时Node结构指针变量x 
    53             x = L->next;//找到头结点,因为链表的查找只能从头开始往后查找 
    54             int sign = 0;
    55             int array[10] = {0};
    56             int i = 0;
    57             int max=0;
    58             while(x){
    59                 if( name == x->name ){
    60                     array[i] = x->score;
    61                     if(max<array[i])
    62                         max = array[i];
    63                     i++;
    64                     sign = 1;
    65                 }    
    66                 x = x->next;
    67             }
    68             if(sign == 1)
    69                 cout<<max<<endl;
    70             if(sign == 0)//如果没有就输出-1 
    71                 cout<<"-1"<<endl;
    72         }
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    UVA1349 Optimal Bus Route Design 最优巴士路线设计
    POJ3565 Ants 蚂蚁(NEERC 2008)
    UVA1663 Purifying Machine 净化器
    UVa11996 Jewel Magic 魔法珠宝
    NEERC2003 Jurassic Remains 侏罗纪
    UVA11895 Honorary Tickets
    gdb调试coredump(使用篇)
    使用 MegaCLI 检测磁盘状态并更换磁盘
    员工直接坦诚直来直去 真性情
    山东浪潮超越3B4000申泰RM5120-L
  • 原文地址:https://www.cnblogs.com/geziyu/p/9915236.html
Copyright © 2011-2022 走看看