2019汇顶科技笔试题2

题目:

编写一个程序实现以下功能:输入跳水比赛中10个评委的打分成绩(注意打分可能有小数),要求统计出其中打分最高的评委人数。

输入描述:

输入为10个数字,以“,”(英文符号)分隔,代表10个评委给出的打分成绩

输出描述:

输出为1个自然数,表示统计出的评委人数。

示例:

输入:7,8,7.6,8.9,8,8.9,8,8.9,8,8,7,8,
输出:2

思路:
方法1:利用红黑树–multiset,其优势在于自动排序,且允许有重复值。本题的难点在于将输入存入string之后且过滤掉逗号之后,需要将string类型转化为double类型:atof(score[i].c_str())。
将string类转化为int型:atoi(score[i].c_str())
将string转化成float型:atof(score[i].c_str())

multiset<double, greater<double>> res;//从大到小排序
multiset<double, less<double>> res;//从小到大排序(默认也是)
temp.erase();//全部清除
temp.erase(i);//下标i之后的全部删掉
temp.erase(i,j);//从下标i开始删除y个元素

程序:

#include<iostream>
#include <string>
#include<vector>
#include <set>
#include <functional>

using namespace std;

int main()
{
        string score_all;//输入的一行数
        cin >> score_all;
        int len_s = score_all.size();
        vector<string> score;//10个打分(string型)
        string temp;

    //------------------将string型字符串以逗号为间隔分成10个string类字符串---------
        for (int i = 0; i < len_s; i++)
        {
            if (score_all[i]!=',')
               temp = temp + score_all[i];//非“,”就一直拼接
            if (score_all[i] == ',')//遇见“,”就压入前面拼接的字符串,并及时将临时变量清空
            { 
                score.push_back(temp);
                temp.erase();
            }

        }
        score.push_back(temp);//最后一次没有“,”,所以还要压入最后一个打分

    //---------------------将string型字符串转化成数字型,顺便排序(自带)-----
    multiset<double, greater<double>> res;
    int len = score.size();
    for (int i = 0; i < len; i++)
    {
        res.insert(atof(score[i].c_str()));
    }

    int count=0;
    //-----------------------遍历----------------------------------------
    auto ibegin = res.begin();
    auto iend = res.end();
    double max = *ibegin;
    for (; ibegin != iend;ibegin++)
    {
        if ((*ibegin - max)<0.001 && (*ibegin - max)>-0.001)//浮点数等值比较需要在一定范围内
            count++;
    }

    cout << count << endl;

    system("pause");
    return 0;
}

方法2:通过自己定义函数Max()找到10个打分的最大值,然后遍历统计最大值的个数。

#include<iostream>
#include <string>
#include<vector>
#include <set>
#include <functional>

using namespace std;


double Max(vector<double> s)
{
    int len = s.size();
    double max = s[0];
    for (int i = 1; i < len; i++)
    {
        if (max<s[i])
            max = s[i];
    }
    return max;
}

int main()
{
    string score_all;
    cin >> score_all;
    int len_s = score_all.size();
    vector<string> score;
    string temp;
//------------------将string型字符串以逗号为间隔分成10个string类字符串---------
    for (int i = 0; i < len_s; i++)
    {
        if (score_all[i]!=',')
           temp = temp + score_all[i];
        if (score_all[i] == ',')
        { 
            score.push_back(temp);
            temp.erase();
        }

    }
    score.push_back(temp);
//---------------------将string型字符串转化成数字型---------------------
    vector<double> s_d;
    int len = score.size();
    for (int i = 0; i < len; i++)
    {
        s_d.push_back(atof(score[i].c_str()));
    }

    int count = 0;
    //--------------------找到最大值----------------------------------
    double max = Max(s_d);

    //--------------------遍历,统计最大值的个数------------------------
    for (int i = 0; i < len; i++)
    {
        if ((s_d[i] - max) > -0.0001 && (s_d[i] - max) < 0.0001)
            count++;
    }

    cout << count << endl;

    system("pause");
    return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值