从网上整理的华为机试题目 下载本文

一、华为机试——2018年校招

10号参加的华为的机试,这次的3道题难度适中。第一题字符串,第二题贪心算法(会不会?),dp可以解,第三题长整数相乘。因为题主做过第三题原题,刚开始就把第三题秒了~~

然后开始做第一题,一般10分钟就可以搞定。第二题要想一下,不过也差不多是原题,Leetcode上第55题jump game应该是此题原型。 因为都刷过几乎是原题的原故,一个小时不到3题就AC完了

下面是完整题目和代码

题目一、给你一个原始字符串,根据该字符串内每个字符出现的次数,按照ASCII码递增顺序重新调整输出。 举例!假设原始字符串为 eeefgghhh

则每种字符出现的次数分别是

1.eee 3次 2.f 1次 3.gg 1次 4.hhh 3次 重新排序后输出的字符串如下: efghegheh

编写程序,实现上述功能。 【温馨提示】

1.原始字符串中仅可能出现“数字”和“字母”; 2.请注意区分字母大小写。

1. #include 2. #include 3. #include 4. #include 5. using namespace std; 6. int main(){ 7. string str; 8. int a[128]={0}; 9. while (cin >> str){

10. for(int i=0;i

12. } //caculate the n

umber of each character

13. int max = *max_element(a, a+128); 14. sort(str.begin(),str.end());

15. long i =unique(str.begin(), str.end()) - str.begin();//the location o

f the \ 16. str=str.substr(0,i); 17. for(int j=0;j

18. for(int i=0;i

24. cout <

27. return 0; 28. }

题目二、给出一组正整数,你从第一个数向最后一个数方向跳跃,每次至少跳跃1格,每个数字的值表示你这个位置可以跳跃的最大长度。计算如何以最少的跳跃次数跳到最后一个数。 输入描述:

第一行表示有多少个数n

第二行依次是1到n,一个数一行。 输出描述:

输出一行,表示跳跃最少的次数

1. #include 2. #include 3. #include 4. #include

5. //确定起点后,用一个指针指向最大跳跃点。

6. //搜索最大跳跃点与起点之间能往后走的最远的点,选定该点为起点,其最大跳跃点为终点。重

复。

7. #define MAX 1000

8. using namespace std; //每次跳的最大长度为a[i]+i。 9. if(a[i] + i > stepMax){ //搜索最大的跳跃点,记为index 10. stepMax = a[i] +i; 11. index =i; 12. }

13. if(i == end){ //搜索完毕,从index(能跳最远的位置)开始继续跳 14. end = stepMax; 15. ++count; 16. i = index; 17. } 18. }

19. return count; 20. } 21.

22. int main() 23. {

24. int n;

25. while (cin >> n){ 26. int a[MAX]={0}; 27. for(int i=0;i> a[i]; 29. }

30. cout <

题目三、编写“长整数相乘”的程序,实现两个任意长度的长整数(正数)相乘,输出结果 这题就不多逼逼了,网上多的是答案。

[

1. #include 2. using namespace std; 3.