• 题目1069:查找学生信息


    题目1069:查找学生信息

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:18114

    解决:4872

    题目描述:

     输入N个学生的信息,然后进行查询。

    输入:

     输入的第一行为N,即学生的个数(N<=1000)

    接下来的N行包括N个学生的信息,信息格式如下:
    01 李江 男 21
    02 刘唐 男 23
    03 张军 男 19
    04 王娜 女 19
    然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
    02
    03
    01
    04
    输出:

     输出M行,每行包括一个对应于查询的学生的信息。

    如果没有对应的学生信息,则输出“No Answer!”
    样例输入:
    4
    01 李江 男 21
    02 刘唐 男 23
    03 张军 男 19
    04 王娜 女 19
    5
    02
    03
    01
    04
    03
    样例输出:
    02 刘唐 男 23
    03 张军 男 19
    01 李江 男 21
    04 王娜 女 19
    03 张军 男 19
    来源:
    2003年清华大学计算机研究生机试真题
    答疑:
    解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7793-1-1.html
    不知道为什么最开始自己写的代码,总是错误,我也很无奈,明明都差不多的。。
    我的错误代码
    #include <stdio.h>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <string>
    #include <string.h>
    #include<iostream>
    using namespace std ;
    
    struct node
    {
        char name[20];
        char sex[10];
        int age;
        char num[20];
    }stu[1010];
    
    int main( )
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s %s %s %d",stu[i].num,stu[i].name,stu[i].sex,&stu[i].age);
        int m;
        scanf("%d",&m);
        while(m--)
        {
            char str[20];
            scanf("
    %s",str);
            int flag=0,num;
            for(int i=0;i<n;i++)
            {
                if(strcmp(str,stu[i].num)==0)
                {
                    num = i;
                    flag = 1;
                    break;
                }
            }
            if(flag)
                printf("%s %s %s %d
    ",stu[num].num,stu[num].name,stu[num].sex,stu[num].age);
            else printf("No Answer!
    ");
        }
        return 0 ;
    }

    后来看了别人的博客,用了动态数组就对了。。。

    #include <stdio.h>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <string>
    #include <string.h>
    #include<iostream>
    using namespace std ;
    
    struct node
    {
        string name;
        string sex;
        int age;
        string num;
    };
    int main( )
    {
        int n;
        while(cin >> n)
        {
            vector<struct node> stu(n);
            for(int i=0; i<n; i++)
                cin >> stu[i].num >> stu[i].name >> stu[i].sex >> stu[i].age;
            int m;
            cin >> m;
            while(m--)
            {
                string str;
                cin >> str;
                int flag=0,num1;
                for(int i=0; i<n; i++)
                {
                    if(str==stu[i].num)
                    {
                        num1 = i;
                        flag = 1;
                        break;
                    }
                }
                if(flag)
                    cout << stu[num1].num << " " << stu[num1].name << " " << stu[num1].sex << " " << stu[num1].age << endl;
                else cout << "No Answer!" << endl;
            }
        }
        return 0 ;
    }

    别人的不用动态数组AC的代码

    #include<stdio.h>  
    #include<string.h>  
    struct node{  
        char num[100];  
        char name[100];  
        char sex[10];  
        int age;     
    }stu[1005];  
       
       
    int main()  
    {  
        int i,j,m,n;  
        char s[10005][100];  
        while(scanf("%d",&n)!=EOF){  
            for(i=0;i<n;i++){  
                scanf("%s %s %s %d",stu[i].num,stu[i].name,stu[i].sex,&stu[i].age);              
            }                        
            scanf("%d",&m);  
            for(i=0;i<m;i++)  scanf("%s",s[i]);  
            for(i=0;i<m;i++){  
                for(j=0;j<n;j++){  
                    if(strcmp(stu[j].num,s[i])==0){  
                        printf("%s %s %s %d
    ",stu[j].num,stu[j].name,stu[j].sex,stu[j].age);   
                        break;                        
                    }              
                }              
                if(j==n) printf("No Answer!
    ");  
            }  
        }  
        return 0;  
    }  
    彼时当年少,莫负好时光。
  • 相关阅读:
    JS 数字时钟的代码(摘录,忘了是从哪了)
    数据写入DataTable C# 2005
    C# 进制转化问题测试下再说(网上的直接转化不好用)
    防sql 注入,就是将sql 的执行命令给排除
    今天研究了一下午网站窄屏/宽屏的切换实现
    解决VS2005下中文输入法全角半角混乱的补丁
    一些实用的站长查询工具
    UE(用户体验)无处不在,留心处处皆学问
    添加了方便聚合的链接
    该好好整理一下自己了
  • 原文地址:https://www.cnblogs.com/l609929321/p/6606278.html
Copyright © 2020-2023  润新知