华为2018 软件题AC

为了快点AC,就没考虑其它的了,实际2小时半的3道题,1小时完全可以做完,这个题目还是比较简单、基础。

1. 歌手评分

题目

不断按行读入格式为“打分数量-分数1-分数2-分数n”的串,去掉最高分、最低分,求平均分,以保留小数点2位输出。

分析

使用vector保存分数,调用库函数sort后,.begin()+1与.end()-1即可去掉最高分、最低分,然后求平均即可。难点在于题目要求是不断按行输入,所以就用个getline 按字符串的形式接收。

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include <iomanip>
#include<string>
using namespace std;

int main()
{
    vector<double> a;
    string line;

    const char *sep = " ";
    char *p;
    while (getline(cin, line)) {
        char temp[20];
        p = strtok(strcpy(temp, line.c_str()), sep);
        while (p) {
            a.push_back(atoi(p));
            p = strtok(NULL, sep);
        }
        sort(a.begin()+1, a.end());
        double sum = accumulate(a.begin() + 2, a.end() - 1, 0);
        double result = sum / (double)(a[0] - 2);
        printf("%0.2f\n", result);
        a.clear();
    }

    return 0;

}

2. 猜数字

题目

通常由两个人玩,一方出数字,一方猜。出数字的人要想好一个没有重复数字的4位数,不能让猜的人知道。猜的人就可以开始猜。每猜一个数字,出数者就要根据这个数字给出几A几B,其中A前面的数字表示位置正确的数的个数,而B前的数字表示数字正确而位置不对的数的个数。

如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。

如上描述,输如格式为“1 2 3 4\n2 3 4 5”的数字,然后输出“0A3B”。

分析

写个for 循环判断下就好。

#include<iostream>

using namespace std;


int judge(int *A, int *B) {
    int a = 0, b = 0;
    for (int i = 0; i < 4; i++) {
        if (*(A + i) == *(B + i)) {
            a++;
        }
        else {
            for (int j = 0; j < 4; j++) {
                if (*(A + i) == *(B + j)) {
                    b++;
                }
            }
        }
    }
    cout << a << "A" << b << "B" << endl;
    return a - b;

}
int main() {
    int standard[4], answer[4];
    for (int j = 0; j < 4; j++) {
        cin >> standard[j];
    }

        for (int j = 0; j < 4; j++) {
            cin >> answer[j];
        }
        judge(standard, answer);
    return 0;
}

3. 青蛙跳过河

题目

输入格式为“10\n2 3 5\n2 3 5 6 7”,其中第一行”10”为河的长度,第二行”2”与“3”表示青蛙跳跃的最小、最大距离,第二行的“5”是指河中石头的位置(以0为起点,10为终点),求青蛙踩石头的最少数目。
青蛙刚好到和跳过都算过桥。

分析

读入的河的长度,按题目描述,有70%的数据值会到10^9,因此我这种开10^3整形数组的方式绝对是不正确的,而且使用的是一个简单迭代,按理说10^9应该会超时,但它就是过了,这算是幸运呢还是不幸呢,反正这个解答我不满意,还请有其它更好解答的大牛们留言告知。

#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include <iomanip>
using namespace std;
int a[10000];
int len;
int s1, s2, num;
vector<int> result;

void go(int i,int count) {
    if (i < len) {

        if (a[i])
            count++;
        for (int j = s1; j <= s2; j++) {
            go(i + j,count);
        }
    }
    if (i >= len) {
        result.push_back(count);
    }
}

int main()
{
    //int len = 0;
    cin >> len;

    cin >> s1 >> s2 >> num;

    for (int i = 0; i < num; i++) {
        int x = 0;
        cin >> x;
        a[x] = 1;
    }

    go(0,0);
    sort(result.begin(), result.end());
    cout << result[0] << endl;
    return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值