华为机试集锦(转载于互联网,谢谢各po)

来源:互联网 发布:厦门seo平台 编辑:程序博客网 时间:2024/04/28 23:03

1成绩排名【编译通过】

  1. #include <iostream>  
  2. using namespace std;  
  3. void bubble(int arr[])  
  4. {int i=0;  
  5.  int j=0;  
  6.  int k=0;  
  7.  for(i=0;i<10;i++)  
  8.  {  
  9.      for(j=0;j<(9-i);j++)  
  10.      {  
  11.          if(arr[j+1]<arr[j])  
  12.          {  
  13.              k=arr[j+1];  
  14.              arr[j+1]=arr[j];  
  15.              arr[j]=k;  
  16.          }  
  17.      }  
  18.  }  
  19. }  
  20.   
  21. int getpassline(int a[])  
  22. {  
  23.     int i=0;  
  24.     bubble(a);  
  25.     if(a[0]>=60)  
  26.     {return 60;}  
  27.     else{  
  28.         return ((a[4]/10)*10);  
  29.     }  
  30. }  
  31.   
  32. int main()  
  33. {int a[10]={0};  
  34. cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9];  
  35. int b=0;  
  36. b=getpassline(a);  
  37. cout<<b<<endl;  
  38. return 0;  
  39. }  
2:拉灯问题

http://blog.csdn.net/dang_jiajia/article/details/37569913 【编译错误】

http://wenku.baidu.com/link?url=aXKuu9LhnnMywlvylZVHqCJV45pnlqCR7zSK4pKyhFWPCdYK9wLlQiJDaeI8aLY3iwvsgHrhld_egrZgIMYeKbB4mjUUOXLKiIK0Q6DrLLi 【编译通过】

3:地铁换乘【编译通过】

http://blog.csdn.net/china_wanglong/article/details/21746215


其他资料

http://wenku.baidu.com/link?url=Wd_bt4egad7fxcvDsPBTniX5BSXeSZa0QQEaO9mmqGrKz7Wa-Tds8jdo7EviA_C18bJKPXinsBx5LKE0e_HHdlbgtYsCfj9IakRBDIbBEOO

http://www.tuicool.com/articles/uyQVV3

http://blog.csdn.net/chenhittler/article/details/11694083

http://blog.csdn.net/kkkkkxiaofei/article/details/22592255

http://www.cnblogs.com/yvictoryr/p/3831232.html

http://bbs.51cto.com/thread-1086982-1.html

http://club.xdnice.com/thread-1320782-1-1.html

http://maidoudao.iteye.com/blog/1680644

http://www.kongrong.com/xiaoyuan/shanxi/1259396.html


2014华为机试西安地区B组试题

题目一、亮着点灯的盏数

一条长廊里依次装有n(1≤n≤65535)盏电灯,从头到尾编号1、2、3、…n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。

有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。

注:电灯数和学生数一致。不能写双重循环,会运行超时。

输入 65535

输出 255

题目分析:

对于任何一盏灯,由于它原来不亮,那么当它的开关被按奇数次时,灯是开着的;当它的开关被按偶数次时,灯是关着的;一盏灯的开关被按的次数,恰等于这盏灯的编号的因数的个数;要求哪些灯还亮着,就是问哪些灯的编号的因数有奇数个.显然完全平方数有奇数个因数。每个数除以一个数A等于另一个数B,那么A和B都是它的因数,于是因数是成对出现的,但是要因数是奇数,就必须A=B所以这个数就必须是一个是的平方得到的。

综上所述这道题非常简单,就是找1-65535中完全平方数的个数。我们利用枚举法

=========================================================================

参考代码:

//亮着点灯的盏数.cpp//2014.7.9 hepanhui#include <iostream>using namespace std;int main(){  int n;  int cnt = 0;  cin >> n;  for(int x = 1; ; x++)  {    int y = x*x;    if(y > n)      break;    cnt ++;  }  cout << cnt << endl;}

调试过程中易错地方,我们要把cnt++放在跳出循环的后面,否则次数会比正确答案多一次。

题目二:相同字符

输入一个字符串,判断是否含有相同的子串(字串长度大于1),是,输出1,否,输出0。

例如12312含有两个12,所以输出1;23456则没有相同子序列,输出0.

输入:12312

输出:1

题目分析 :这个太简单了,用哈希表检测键值存在>1说明就有相同字符。

=======================================================================

参考代码:

//相同字符.cpp//2014.7.9 hepanhui#include <iostream>#include <string>const int TableSize = 256;using namespace std;int Equalchar(char *str){  int hashTable[TableSize];  for(int i = 0; i < TableSize; i++)    hashTable[i] = 0;  char *phashKey = str;  while(*phashKey)  {    hashTable[*phashKey]++;    phashKey++;  }  phashKey = str;  while(*phashKey)  {    if(hashTable[*phashKey] > 1)      return 1;    phashKey++;  }  return 0;}int main(){  char s[1000];  gets(s);  cout << Equalchar(s) << endl;  return 0;}

调试过程易错的地方:

①输入字符串有可能有空格。所以不能用cin

②用gets(s)一定要注意加上头文件#include<string>

③第二次循环的phashKey++一定不能忘记。

题目三、整数相除

两个整数相除,将结果用字符串返回。如果是循环小数,将循环的位用括号括起来。

函数原型为 void div(const int a,const int b,char *str)

输入:1 3

输出:0.(3)

题目分析:

这道题比循环小数的题目多有一点点东西,那就是要判断ab的正负性,其他都一样。

难点在于循环循环小数的周期和循环长度,所以我们这里定义两个数组

int reminder_exist[max_INT];int reminder_pos[max_INT];

==============================================================================

参考代码:

//整数相除.cpp//2014.7.9 hepanhui#include<iostream>#include<string>using namespace std;const int maxn = 100; //设置字符串的最大位数const int max_INT = 10000; int reminder_exist[max_INT];int reminder_pos[max_INT];void div(const int a, const int b, char *str){  int numerator,denominator,quotient, reminder, outcnt = 0;  int flag = 0; //负数标志  int original_numerator = a; //求整数部分用到的分子  memset(reminder_exist, 0, sizeof(reminder_exist));  memset(reminder_pos, 0, sizeof(reminder_pos));  numerator = a; //由于定义const int所以我们要改变分子分母时候,所以我们通过中间变量转化  denominator = b;  if(a*b < 0)    flag = 1;  //将分子和分母变成整数  numerator = numerator < 0 ? -numerator:numerator;   denominator = denominator < 0 ? -denominator:denominator;  quotient = numerator/denominator;  reminder = numerator%denominator;     int integer = quotient;  //找出循环  //int found_cycle = 0;   int cycle_pos = maxn; //循环的位置  int cycle_len = 0; //初始化循环长度  int i = 0;  for(i = 0; i <= maxn; i++)  {    //找出余数相等的情况,求出循环周期    if(reminder_exist[reminder])    {      cycle_pos = reminder_pos[reminder];      cycle_len = i - cycle_pos;      break;    }    else    {      reminder_exist[reminder] = 1;      reminder_pos[reminder] = i;    }    numerator = reminder *10;    quotient = numerator/denominator;    reminder = numerator%denominator;     str[outcnt++] = quotient + '0'; //将更新的商存入字符串中  }  str[outcnt++] = '\0';  if(!flag)  {    cout << integer << "." ;  }  else    cout << "-" << integer << ".";  for(int j = 0; j < cycle_pos; j++)    cout << str[j];  cout << "(";  for(int j = cycle_pos; j < i;j++)      cout <<  str[j];  cout << ")" << endl;}int main(){  int a,b,flag = 0;  char s[maxn];  cin >> a >> b;  if(!a && !b)    return 0;  div( a, b,s);  return 0;}

调试过程易犯的错误:

①主函数和子函数变量的设置,和非法输入的安排自己比较混乱;

②讨论负数的情况,直接要将负数变成整数来循环,学会用 ? : 三目运算符

③定义输出是字符串型,我们不能直接str[outcnt++] = quotient;因为quotient是一个数,所以我们必须要加上+‘0’

④仍然需要提醒的是字符串数组存入的题目,要注意

1)outcnt要初始化为0

2)str[outcnt++]中++不能忘记

3)str[outcnt++] = '\0'

4)主函数定义的时候char str[maxn];

5)这道题目输出格式需要仔细书写。


0 0
原创粉丝点击