华为2018安卓实习笔试题

第一题——RAKE多径

题意

在无线通信接收,接收到的信号一般会有RAKE多径。
现给出RAKE接收机21径能量;多径索引为0~20;
用一个4径的接收窗在21径上进行滑动,滑动窗判断什么位置接收最大能量。接收窗索引以窗内第一径的索引为准。
比如窗最大能力框住径为0,1,2,3;则窗索引为0;
如果两个窗的能量一样,则以索引最小为输出。
要求输出:最大接收能量窗的索引

输入描述:
21径能量,每一径能量范围为【0,65536】

输出描述:
最大接收能量窗的索引

输入

0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
17
1
1
12

输出

17

思路

不知道你们看懂题意了没有,一开始我以为是要在21个整数里找到最大那个的索引,后来我才明白了,是4个整数一组,连续滑动,所以我能想到的就是从索引0到17遍历,每次都要加上后面三个数字,然后不断判断大小,最后取最大值,但是这种思路只能过16.67%的样例。这个历史难题只能交给时间来解答了。

代码

# include<stdio.h>
# include<iostream>
# include<stack>
# include<memory.h>
# include<vector>
# include<map>
# include<algorithm>
# include<list>
# include<queue>
# include<set>
# include<string.h>
# include<cstring>
# include<math.h>

using namespace std;

int main(void) {
    long long suoyin[21];
    long long  max = 0;
    for (int i = 0; i<=20; i++) {
        scanf("%lld", &suoyin[i]);
    }
    long long res;
    for (int i = 0; i<=17; i++) {
        res = suoyin[i] + suoyin[i+1] + suoyin[i+2] + suoyin[i+3];
        if (res > max) {
            max = i;
        }
    }
    printf("%lld\n", max);
    return 0;
}

第二题——解密自然键盘

题意

“自然键盘”的按键顺序是字母顺序,我们通常用的是“标准键盘”,字母排布顺序是打乱的。
也就是标准键盘“QWER……”对应自然键盘“ABCD”
一个可以在标准键盘上盲打的小学生,并没有注意到自己使用的是自然键盘,所以盲打了“I am a boy.”结果输出的是“H kz k xif.”
题目要求把自然键盘上产生的“H kz k xif.”这样的字符串转换为标准键盘上的“I am a boy.”
要求输出:最大接收能量窗的索引

输入描述:
一个字符串,表示按照标准键盘输入动作,在自然键盘上打字产生的字符串。

输出描述:
打字的人在标准键盘上要打印的实际内容

输入

H kz k xif.

输出

I am a boy.

思路

只要对所有输入的字母进行一下判断即可,务必注意一点:不是输入一个标准键盘字符串转换为自然键盘字符串,而是反过来。所以不是把输入的Q转为A,而是把输入的A转为Q。
通过样例100%。

代码

# include<stdio.h>
# include<iostream>
# include<stack>
# include<memory.h>
# include<vector>
# include<map>
# include<algorithm>
# include<list>
# include<queue>
# include<set>
# include<string.h>
# include<cstring>
# include<math.h>

using namespace std;

int main(void) {
    string shuru;
    string res;
    getline(cin, shuru);
    for (int i = 0; i<=shuru.length(); i++) {
        if (shuru[i] == 'A'){
            shuru[i] = 'Q';
        } else if (shuru[i] == 'a') {
            shuru[i] = 'q';
        } else if (shuru[i] == 'B') {
            shuru[i] = 'W';
        } else if (shuru[i] == 'b') {
            shuru[i] = 'w';
        } else if (shuru[i] == 'C') {
            shuru[i] = 'E';
        } else if (shuru[i] == 'c') {
            shuru[i] = 'e';
        } else if (shuru[i] == 'D') {
            shuru[i] = 'R';
        } else if (shuru[i] == 'd') {
            shuru[i] = 'r';
        } else if (shuru[i] == 'E') {
            shuru[i] = 'T';
        } else if (shuru[i] == 'e') {
            shuru[i] = 't';
        } else if (shuru[i] == 'F') {
            shuru[i] = 'Y';
        } else if (shuru[i] == 'f') {
            shuru[i] = 'y';
        } else if (shuru[i] == 'G') {
            shuru[i] = 'U';
        } else if (shuru[i] == 'g') {
            shuru[i] = 'u';
        } else if (shuru[i] == 'H') {
            shuru[i] = 'I';
        } else if (shuru[i] == 'h') {
            shuru[i] = 'i';
        } else if (shuru[i] == 'I') {
            shuru[i] = 'O';
        } else if (shuru[i] == 'i') {
            shuru[i] = 'o';
        } else if (shuru[i] == 'J') {
            shuru[i] = 'P';
        } else if (shuru[i] == 'j') {
            shuru[i] = 'p';
        } else if (shuru[i] == 'K') {
            shuru[i] = 'A';
        } else if (shuru[i] == 'k') {
            shuru[i] = 'a';
        } else if (shuru[i] == 'L') {
            shuru[i] = 'S';
        } else if (shuru[i] == 'l') {
            shuru[i] = 's';
        } else if (shuru[i] == 'M') {
            shuru[i] = 'D';
        } else if (shuru[i] == 'm') {
            shuru[i] = 'd';
        } else if (shuru[i] == 'N') {
            shuru[i] = 'F';
        } else if (shuru[i] == 'n') {
            shuru[i] = 'f';
        } else if (shuru[i] == 'O') {
            shuru[i] = 'G';
        } else if (shuru[i] == 'o') {
            shuru[i] = 'g';
        } else if (shuru[i] == 'P') {
            shuru[i] = 'H';
        } else if (shuru[i] == 'p') {
            shuru[i] = 'h';
        } else if (shuru[i] == 'Q') {
            shuru[i] = 'J';
        } else if (shuru[i] == 'q') {
            shuru[i] = 'j';
        } else if (shuru[i] == 'R') {
            shuru[i] = 'K';
        } else if (shuru[i] == 'r') {
            shuru[i] = 'k';
        } else if (shuru[i] == 'S') {
            shuru[i] = 'L';
        } else if (shuru[i] == 's') {
            shuru[i] = 'l';
        } else if (shuru[i] == 'T') {
            shuru[i] = 'Z';
        } else if (shuru[i] == 't') {
            shuru[i] = 'z';
        } else if (shuru[i] == 'U') {
            shuru[i] = 'X';
        } else if (shuru[i] == 'u') {
            shuru[i] = 'x';
        } else if (shuru[i] == 'V') {
            shuru[i] = 'C';
        } else if (shuru[i] == 'v') {
            shuru[i] = 'c';
        } else if (shuru[i] == 'W') {
            shuru[i] = 'V';
        } else if (shuru[i] == 'w') {
            shuru[i] = 'v';
        } else if (shuru[i] == 'X') {
            shuru[i] = 'B';
        } else if (shuru[i] == 'x') {
            shuru[i] = 'b';
        } else if (shuru[i] == 'Y') {
            shuru[i] = 'N';
        } else if (shuru[i] == 'y') {
            shuru[i] = 'n';
        } else if (shuru[i] == 'Z') {
            shuru[i] = 'M';
        } else if (shuru[i] == 'z') {
            shuru[i] = 'm';
        }
    }
    cout << shuru << endl;
    return 0;
}

第三题——数字求和

题意

输入是一个整型数组,数组里的整型数可以是正数,也可以是负数。子数组指的是数组中连续的一个或者多个元素的集合,求这些子数组中,求和后最大值是多少。

输入描述:
一个整型数组

输出描述:
最大连续子数组的和

输入

2,-3,4,11,-5,8,3,-6

输出

21(4+11-5+8+3)

思路

这道题是求最大子串和,所以可以考虑用动态规划来做。
思想就是,把每个数字想得很人性化很自私,他们都想把自己的值往上加好参与到最终的输出结果来。那么如果轮到我的时候,我前面的那段是正的(大于0),那么我就可以把它们加上,抱他们的大腿,这样对我来说绝对是赚的;如果他们小于0,加上他们后我反而要下降,这不能接受,要求的是最大的,所以如果前面小于0,那我就不要,我自己做我自己。
然后后面的人也这么想,每个数字都去这么想。
比如2,-3,4,11,-5,8,3,-6
第一个是2,那么当前就是2(2因为前面没人,所以自立门户)
第二个是-3,对他来说,2大于0,所以他要抱2的大腿,变成-1
第三个是4,4的前面那段是-1,所以他不要,他自立门户所以是4
第四个是11,11前面是4,大于0的他很喜欢,所以加上,现在是15
第五个是-5,-5看到抱大腿所以是10
第六个是8,前面是10所以是18
第七个是3,所以是21
第八个是-6,所以是15
那么找到2,-1,4,15,10,18,21,15中最大的21即可(这个其实维护一个变量每次都去判断就行了)
然而你以为这样就结束了吗,华为告诉你,其实输入的不是一个整型数组,而是一个字符串!所以你还要去抽取字符串中,逗号前的数字,不要忘记有负号的负数!
通过样例80%(还有20%我真的不知道错哪了,望时间能解答)。

代码

# include<stdio.h>
# include<iostream>
# include<stack>
# include<memory.h>
# include<vector>
# include<map>
# include<algorithm>
# include<list>
# include<queue>
# include<set>
# include<string.h>
# include<cstring>
# include<math.h>
# include<limits.h>

using namespace std;
//2, -3, 4, 11, -5, 8, 3, -6
int main(void) {
    string shuru;
    vector<long long> shuzi;
    long long num;
    long long currentmax = 0;
    long long res = INT_MIN;
    getline(cin, shuru);
    int isfu = 1;
    long long number = 0;
    for (int i=0; i<shuru.length(); i++) {
        if (shuru[i] == '-') {
            isfu = -1;
        } else if (shuru[i] == ',') {
            // 遇到逗号则提交,记得初始化两个变量 
            shuzi.push_back(isfu*number);
            isfu = 1;
            number = 0;
        } else if (shuru[i] >= '0' && shuru[i] <= '9'){
            number = number*10 + shuru[i]-'0';
        }
        if (i == shuru.length()-1) {
            shuzi.push_back(isfu*number);
        }
    }
    for (int i=0; i<shuzi.size(); i++) {
        num = shuzi[i];

        if (currentmax < 0) {
            // 如果前面的哥们小于0,自立门户 
            currentmax = num;
        } else {
            // 否则抱大腿 
            currentmax += num;
        }

        if (currentmax > res) {
            // 判断最大和的结果 
            res = currentmax;
        }
    }
    printf("%lld\n", res);

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值