zoukankan      html  css  js  c++  java
  • 考研机试练习(KY2KY10)

    KY2 成绩排序

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M

    本题知识点: 排序 sort struct

    题目描述

    查找和排序

    题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
    都按先录入排列在前的规则处理。

    示例:
    jack 70
    peter 96
    Tom 70
    smith 67

    从高到低 成绩
    peter 96
    jack 70
    Tom 70
    smith 67

    从低到高

    smith 67
    jack 70
    Tom 70
    peter 96

    输入描述:

    输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开。

    输出描述:

    按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

    输入

    3
    0
    fang 90
    yang 50
    ning 70
    

    输出

    fang 90
    ning 70
    yang 50
    

    代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    struct Student{
        string name;
        int score;
        int order;
    };
    
    bool CompareDescending(Student x, Student y){
        if(x.score == y.score){
            return x.order < y.order;
        }else{
            return x.score > y.score;
        }
    }
    
    bool CompareAscending(Student x, Student y){
        if(x.score == y.score){
            return x.order < y.order;
        }else{
            return x.score < y.score;
        }
    }
    
    int main() {
        int n;
        int type;
        while (scanf("%d%d",&n,&type)!= EOF) {
            Student stu[n];
            for (int i = 0; i < n; i++) {
                cin >> stu[i].name >> stu[i].score;
                stu[i].order = i;
            }
            if (type) {
              sort(stu,stu+n,CompareAscending);
            }
            else {//为0就降序输出
                 sort(stu,stu+n,CompareDescending);
            }
            for(int i=0; i < n; ++i){
                cout<< stu[i].name<< " " << stu[i].score <<endl;
            }
        }
        return 0;
    }
    

    KY3 约数的个数

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M

    本题知识点: 数学 穷举 sqrt(i) 因数

    题目描述

    输入n个整数,依次输出每个数的约数的个数

    输入描述:

    输入的第一行为N,即数组的个数(N<=1000)
    接下来的1行包括N个整数,其中每个数的范围为(1<=Num<=1000000000)

    输出描述:

    可能有多组输入数据,对于每组输入数据,
    输出N行,其中每一行对应上面的一个数的约数的个数。

    输入

    5
    1 3 4 6 12
    

    输出

    1
    2
    3
    4
    6
    

    代码

    #include<iostream>
    #include<math.h>
    using namespace std;
    int main(){
        int n,i;
        while(cin>>n){
            if(n==0)break;
            while(n--){
                cin>>i;
                int sum=0;
                int sq=sqrt(i);
                if(sq*sq==i)sum-=1;
                for(int j=1;j<=sq;j++){
                    if(i%j==0){
                        sum+=2;
                    }
                }cout<<sum<<endl;
            }
        }
        return 0;
    }
    

    KY4 代理服务器

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M

    本题知识点: 贪心 二分

    题目描述

    使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私。我们知道n个代理服务器的IP地址,现在要用它们去访问m个服务器。这 m 个服务器的 IP 地址和访问顺序也已经给出。系统在同一时刻只能使用一个代理服务器,并要求不能用代理服务器去访问和它 IP地址相同的服务器(不然客户端信息很有可能就会被泄露)。在这样的条件下,找到一种使用代理服务器的方案,使得代理服务器切换的次数尽可能得少。

    输入描述:

    每个测试数据包括 n + m + 2 行。

    第 1 行只包含一个整数 n,表示代理服务器的个数。
    第 2 行至第n + 1行每行是一个字符串,表示代理服务器的 IP地址。这n个 IP地址两两不相同。
    第 n + 2 行只包含一个整数 m,表示要访问的服务器的个数。
    第 n + 3 行至第 n + m + 2 行每行是一个字符串,表示要访问的服务器的 IP 地址,按照访问的顺序给出。
    每个字符串都是合法的IP地址,形式为“xxx.yyy.zzz.www”,其中任何一部分均是0–255之间的整数。输入数据的任何一行都不包含空格字符。
    其中,1<=n<=1000,1<=m<=5000。

    输出描述:

    可能有多组测试数据,对于每组输入数据, 输出数据只有一行,包含一个整数s,表示按照要求访问服务器的过程中切换代理服务器的最少次数。第一次使用的代理服务器不计入切换次数中。若没有符合要求的安排方式,则输出-1。

    输入

    3
    166.111.4.100
    162.105.131.113
    202.112.128.69
    6
    72.14.235.104
    166.111.4.100
    207.46.19.190
    202.112.128.69
    162.105.131.113
    118.214.226.52
    

    输出

    1
    

    代码

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    using namespace std;
    map<string, bool> agent;
    void reset()
    {
        for (auto it = agent.begin(); it != agent.end(); it++)
            (*it).second = true;
    }
    bool all_false()
    {
        for (auto it = agent.begin(); it != agent.end(); it++)
            if ((*it).second == true)
                return false;
        return true;
    }
    int main()
    {
        int n, m;
        while (cin >> n)
        {
            string tmp;
            for (int i = 0; i < n; i++)
            {
                cin >> tmp;
                agent[tmp] = true;
            }
            cin >> m;
            int cnt = 0;
            for (int i = 0; i < m; i++)
            {
                cin >> tmp;
                if (agent.count(tmp) == 1)
                {
                    agent[tmp] = false;
                    if (all_false() == true)
                    {
                        reset();
                        agent[tmp] = false;
                        cnt++;
                    }
                }
            }
            if (agent.size() == 1 && cnt != 0)
                cout << -1 << endl;
            else
                cout << cnt << endl;
        }
    }
    

    KY6 手机键盘

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M

    题知识点: 字符串 模拟

    题目描述

    按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。 如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下 如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。 现在给出一串字符,需要计算出它所需要花费的时间。

    输入描述:

    一个长度不大于100的字符串,其中只有手机按键上有的小写字母

    输出描述:

    输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间

    输入

    bob
    www
    

    输出

    7
    7
    

    代码

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        int key[26] = {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};
        string str;
        while(cin>>str)
        {
            int count = key[str[0]-'a'];
            for(int i=1;i<str.size();++i)
            {
                count += key[str[i]-'a'];
                if(key[str[i]-'a']-key[str[i-1]-'a']==str[i]-str[i-1])//判断是否在同一个按键上
                    count+=2;
            }
            cout<<count<<endl;
        }
    }
    

    KY7 质因数的个数

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M

    本题知识点: 数学

    题目描述

    求正整数N(N>1)的质因数的个数。 相同的质因数需要重复计算。如\(120=2*2*2*3*5\),共有5个质因数。

    输入描述:

    可能有多组测试数据,每组测试数据的输入是一个正整数N,(\(1<N<10^9\))。

    输出描述:

    对于每组数据,输出N的质因数的个数。

    输入

    120
    

    输出

    5
    

    代码

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main(){
        //这题的关键:
        //1、是sqrt,可以极大减少复杂度,若是到方根N仍大于1,则必还有且只还有1个质因数
        //2、每次瞬间整除都可帮助减少遍历范围
        long M=100;
        while(cin>>M)
        {
            long count=0;
                for(long j=2;j<=sqrt(M);j++)
                {
                 while(M%j==0)
                 {
                     M=M/j;
                     count++;
                 }
                 if(M<=1)break;
                }
            if(M>1)count++;
            cout<<count<<endl;
        }   
        return 0;
    }
    

    KY8 整数拆分

    题目描述

    一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1+1+2+2 7=1+1+1+1+1+2 7=1+1+1+1+1+1+1 总共有六种不同的拆分方式。 再比如:4可以拆分成:4 = 4,4 = 1 + 1 + 1 + 1,4 = 2 + 2,4=1+1+2。 用f(n)表示n的不同拆分的种数,例如f(7)=6. 要求编写程序,读入n(不超过1000000),输出f(n)%1000000000。

    输入描述:

    每组输入包括一个整数:\(N(1<=N<=1000000)\)

    输出描述:

    对于每组数据,输出f(n)%1000000000。

    输入

    7
    

    输出

    6
    

    代码

    #include <iostream>
    #define MaxSize 1000000
    using namespace std;
     
    int main() {
        int a[MaxSize + 1], N;
        a[0] = a[1] = 1;
        for (int i = 2; i <= MaxSize; i++)
            if (i % 2 == 0)
                a[i] = (a[i - 1] + a[i / 2]) % 1000000000;
            else
                a[i] = a[i - 1];
        while (cin >> N)
            cout << a[N] << endl;
    }
    

    KY9 成绩排序

    题目描述

    用一维数组存储学号和成绩,然后,按成绩排序输出。

    输入描述:

    输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
    接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。

    输出描述:

    按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
    如果学生的成绩相同,则按照学号的大小进行从小到大排序。

    输入

    3
    1 90
    2 87
    3 92
    

    输出

    2 87
    1 90
    3 92
    

    代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using  namespace std;
    
    struct Student{
        int number;
        int score;
    };
    
    const int MAXN = 100;
    
    Student arr[MAXN];
    
    //按照学号升序和成绩升序的比较函数
    bool Compare(Student x, Student y){
        if(x.score == y.score){
            return x.number < y.number;
        } else {
            return x.score < y.score;
        }
    }
    int main(){
        int n;
        scanf("%d",&n);
        for(int i = 0; i < n; ++i){
            scanf("%d%d", &arr[i].number, &arr[i].score);
        }
        sort(arr,arr+n,Compare);
        for(int i = 0; i < n; ++i){
            printf("%d %d\n", arr[i].number, arr[i].score);
        }
        return 0;
    }
    

    KY10 球的半径和体积

    题目描述

    输入球的中心点和球上某一点的坐标,计算球的半径和体积。

    输入描述:

    球的中心点和球上某一点的坐标,以如下形式输入:x0 y0 z0 x1 y1 z1

    输出描述:

    输入可能有多组,对于每组输入,输出球的半径和体积,并且结果保留三位小数

    为避免精度问题,PI值请使用arccos(-1)。

    输入

    0 0 0 1 1 1 
    

    输出

    1.732 21.766
    

    代码

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int main(){
       int a,b,c,d,e,f;
        double g;
        while(cin>>a){
            cin>>b>>c>>d>>e>>f;       
            g=(double)((d-a)*(d-a)+(e-b)*(e-b)+(f-c)*(f-c));
            g=sqrt(g);      
            cout<< fixed <<setprecision(3)<<g<<" "<<acos(-1)*g*g*g*4/3<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    HDU 1290 献给杭电五十周年校庆的礼物(面分割空间 求得到的最大空间数目)
    2018年暑假ACM个人训练题6 解题报告
    HDU 2096 小明A+B(%的运用)
    HDU 2097 sky数 (进制转化)
    布局(codevs 1242)
    Remmarguts’ Date(poj 2449)
    魔法猪学院(codevs 1835)
    统计单词个数(codevs 1040)
    小L 的二叉树(洛谷 U4727)
    Shortest Prefixes(poj 2001)
  • 原文地址:https://www.cnblogs.com/zhangzizi/p/14350847.html
Copyright © 2011-2022 走看看