目录

​2017年4月21日华为笔试题 圣诞的祝福​

​2017年4月21日华为笔试题 德州扑克​

​2017年4月21日华为笔试题 日期的天数序号​

​2017华为笔试题 任务调度​

​2017华为笔试题 公司年会​

​2017华为笔试题 水仙花数​

​2018华为笔试题​

​2018华为笔试题2​


2017年4月21日华为笔试题 圣诞的祝福


  • 简要描述: 给定一个M行N列的矩阵(M*N个格子),每个格子中放着一定数量的平安果。
    你从左上角的格子开始,只能向下或向右走,目的地是右下角的格子。
    每走过一个格子,就把格子上的平安果都收集起来。求你最多能收集到多少平安果。
    注意:当经过一个格子时,需要要一次性把格子里的平安果都拿走。
    限制条件:1 < N, M <= 50;每个格子里的平安果数量是0到1000(包含0和1000)。
  • 输入包括两行:
    第一行为矩阵的行数M和列数N
    第二行为一个M*N的矩阵,矩阵的数字代表平安果的数量,例如:
    1 2 3 40
    6 7 8 90
  • 输出一个数字,表示能收集到的平安果的数量

华为笔试2017年4月21日华为笔试题 圣诞的祝福_笔试题

感觉这题很简单,直接一个动态规划的备忘录就解决了。

如果运行出现数组越界的情况,有2种情况

第一,访问了类似ans[-1][3]或者ans[3][-1]这样的

第二,数组刚好开小了一点点,然后当m=50,n=50的时候刚好越界

代码:

#include <iostream>
using namespace std;

int m, n, num[50][50], ans[50][50];

int f(int row, int line)
{
if (ans[row][line] >= 0)return ans[row][line];
if (row == 0 && line == 0)return num[0][0];
if (row == 0)ans[row][line] = f(row, line - 1) + num[row][line];
else if (line == 0)ans[row][line] = f(row - 1, line) + num[row][line];
else
{
ans[row][line] = f(row, line - 1) + num[row][line];
int k = f(row - 1, line) + num[row][line];
if (ans[row][line] < k)ans[row][line] = k;
}
return ans[row][line];
}

int main()
{
cin >> m >> n;
for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)
{
cin >> num[i][j];
ans[i][j] = -1;
}
cout << f(m - 1, n - 1);
return 0;
}

2017年4月21日华为笔试题 德州扑克

题目:

华为笔试2017年4月21日华为笔试题 圣诞的祝福_笔试题_02

华为笔试2017年4月21日华为笔试题 圣诞的祝福_i++_03

华为笔试2017年4月21日华为笔试题 圣诞的祝福_i++_04

如果输入10,只读取1,0不管,这样就可以用"234567891JQKA"这13个字母表示13张牌了,写代码方便很多。

然后主要就是要理清楚这7种牌型之间的关系,首先按照最多有多少个数字是一样的,可以分成3大类。

第一类,有4个相同数字,一定是牌型2

第二类,有3个相同数字,是牌型3或6

第三类,没有任何3个数字相同,是牌型1或4或5或7

然后再细分就很简单。

代码:

#include <iostream>
using namespace std;

char num[5], color[5];
const char an[14] = "234567891JQKA";

bool num34(int n)//刚好有3或者4个相同的数字
{
char c;
for (int i = 0; i < 5; i++)
{
c = num[i];
int s = 0;
for (int i = 0; i < 5; i++)if (num[i] == c)s++;
if (s == n)return true;
}
return false;
}

bool shun()//是不是顺子
{
int m = 14;
for (int i = 0; i < 5; i++)for (int j = 0; j < 13; j++)if (num[i] == an[j] && m>j)m = j;
for (int j = m; j < m + 5; j++)
{
bool flag = true;
for (int i = 0; i < 5; i++)if (num[i] == an[j])flag = false;
if (flag)return false;
}
return true;
}

bool tonghua()//是不是同花
{
for (int i = 1; i < 5; i++)if (color[i] != color[0])return false;
return true;
}

int f()
{
if (num34(3))
{
char c;
for (int i = 0; i < 5; i++)
{
c = num[i];
int s = 0;
for (int i = 0; i < 5; i++)if (num[i] == c)s++;
if (s == 1)return 6;
}
return 3;
}
if (num34(4))return 2;
if (shun())
{
if (tonghua())return 1;
return 5;
}
if (tonghua())return 4;
return 7;
}

int main()
{
char c;
for (int i = 0; i < 5; i++)
{
cin >> num[i];
if (num[i] == '1')cin >> c;
cin >> color[i];
}
cout << f();
return 0;
}

2017年4月21日华为笔试题 日期的天数序号

题目:

华为笔试2017年4月21日华为笔试题 圣诞的祝福_#include_05

华为笔试2017年4月21日华为笔试题 圣诞的祝福_#include_06

华为笔试2017年4月21日华为笔试题 圣诞的祝福_笔试题_07


这个题目真的挺坑的,明明这么简单的题目,提交一直不对,还好我有一定的ACM经历,有几次碰到过这种情况,最后把最后的点删掉就可以了

代码:

#include <iostream>
using namespace std;

bool r(int year)//第year年是不是闰年
{
if (year % 4)return false;
if (year % 100)return true;
return year % 400 == 0;
}

int main()
{
int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int y, m, d, s;
char c;
cin >> y >> c >> m >> c >> d;
if (r(y))day[2]++;
if (y <= 0 || y >= 10000 || m <= 0 || m > 12 || d <= 0 || d > day[m])
cout << "invalid input";
else
{
s = d;
for (int i = 1; i < m; i++)s += day[i];
printf("%d-%d-%d is the No.%d day of %d",y,m,d,s,y);
}
return 0;
}

2017华为笔试题 任务调度

题目描述:

输入5个任务的ID,任务优先级,开始运行时间和任务运行时间,其中调度过程支持抢占,即优先级值高的任务可抢占正在运行的优先级值低的任务,最后输出前200秒,任务的调度过程

输入描述:

任务信息输入格式为:[任务ID.任务优秀级.任务开始运行时间.任务运行时长];任务与任务之间使用“|”隔离;5个任务的任务ID为1-5;任务优先级范围为0-200.

输出描述:

任务ID.任务运行时长。任务与任务之间使用‘|’分割,无任务运行时ID为0。

输入例子:

[1.80.1.10]|[2.20.11.15]|[3.50.21.10]|[4.120.31.10]|[5.100.41.10]

输出例子:

0.1|1.10|2.10|3.10|4.10|5.10|2.5|0.144


这个似乎不难,不过写起来倒也麻烦。

反正不是我笔试,懒得写了。

2017华为笔试题 公司年会

题目:

华为笔试2017年4月21日华为笔试题 圣诞的祝福_i++_08

华为笔试2017年4月21日华为笔试题 圣诞的祝福_i++_09


这个题目如果用递归的函数的话,主要就是要避免死循环

代码(没地方提交,不知道对不对):

#include <iostream>
#include<algorithm>
using namespace std;

int list[30], key = 0;

void add(int a)
{
if (key >= 30)return;
for (int i = 0; i < key; i++)if (list[i] == a)return;
list[key++] = a;
}

void add(int a, int b, int c)
{
add(a), add(b), add(c);
add(a * 10 + b), add(a * 10 + c);
add(b * 10 + a), add(b * 10 + c);
add(c * 10 + a), add(c * 10 + b);
}

void add25(int a, int b, int c)//只考虑2和5
{
add(a, b, c);
if (a == 2 || a == 5)add(7 - a, b, c);
if (b == 2 || b == 5)add(a, 7 - b, c);
if (c == 2 || c == 5)add(a, b, 7 - c);
}

void add69(int a, int b, int c)//只考虑6和9
{
add25(a, b, c);
if (a == 6 || a == 9)add25(15 - a, b, c);
if (b == 6 || b == 9)add25(a, 15 - b, c);
if (c == 6 || c == 9)add25(a, b, 15 - c);
}

int main()
{
int a, b, c;
scanf("%d,%d,%d", &a, &b, &c);
int m = a;
if (m < b)m = b;
if (m < c)m = c;
add69(a, b, c);
sort(list, list + key);
cout << list[m - 1];
return 0;
}

2017华为笔试题 水仙花数

题目描述:水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。(例如:1³+5³+3³=153),要求:输出1000范围内所有水仙花数及水仙花数之和。

输入描述:无

输出描述:1000以内所有水仙花数及总和。

输出例子:

第1个水仙花数:xxx

第2个水仙花数:xxx

第3个水仙花数:xxx

...

水仙花数总和为:xxx


代码(没地方提交,不知道对不对):

#include <iostream>
using namespace std;

/*
bool narcissus(int n)
{
int a = n / 100, b = n / 10 % 10, c = n % 10;
return a*a*a + b*b*b + c*c*c == a * 100 + b * 10 + c;
}

int main()
{
int sum = 0;
for (int i = 100; i < 1000; i++)if (narcissus(i))
{
cout << i << endl;
sum += i;
}
cout << sum;
return 0;
}
*/

int main()
{
cout << "第1个水仙花数:" << 153 << "\n第2个水仙花数:" << 370 << "\n第3个水仙花数:" << 371 << "\n第4个水仙花数:" << 407 << "\n水仙花数总和为:" << 1301;
return 0;
}

2018华为笔试题

华为笔试2017年4月21日华为笔试题 圣诞的祝福_笔试题_10

华为笔试2017年4月21日华为笔试题 圣诞的祝福_i++_11

华为笔试2017年4月21日华为笔试题 圣诞的祝福_笔试题_12


代码:

#include<iostream>
using namespace std;

int main()
{
long long n, M, N, list[51];
cin >> n;
list[0] = 1, list[1] = 2, list[2] = 3, list[3] = 4;
for (int i = 4; i <= 50; i++)list[i] = list[i - 1] + list[i - 4];
while (n--)
{
cin >> M >> N;
cout << list[N] * M << endl;
}
return 0;
}

2018华为笔试题2

华为笔试2017年4月21日华为笔试题 圣诞的祝福_#include_13

华为笔试2017年4月21日华为笔试题 圣诞的祝福_i++_14

示例1

输入

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

输出

0

2

0

4

0

6

0

8

0

10

0

8

0

6

0

4

0

2

代码:

#include<iostream>
using namespace std;

struct node
{
int shi, xu;
}nod[11];

int getshi(int a, int b)
{
return nod[a].shi*nod[b].shi - nod[a].xu*nod[b].xu;
}

int getxu(int a, int b)
{
return nod[a].shi*nod[b].xu + nod[a].xu*nod[b].shi;
}

int main()
{
for (int i = 1; i <= 10; i++)cin >> nod[i].shi >> nod[i].xu;
cout << getshi(1, 6) << endl;
cout << getxu(1, 6) << endl;
cout << getshi(1, 7) + getshi(2, 6) << endl;
cout << getxu(1, 7) + getxu(2, 6) << endl;
cout << getshi(1, 8) + getshi(2, 7) + getshi(3, 6) << endl;
cout << getxu(1, 8) + getxu(2, 7) + getxu(3, 6) << endl;
cout << getshi(1, 9) + getshi(2, 8) + getshi(3, 7) + getshi(4, 6) << endl;
cout << getxu(1, 9) + getxu(2, 8) + getxu(3, 7) + getxu(4, 6) << endl;
cout << getshi(1, 10) + getshi(2, 9) + getshi(3, 8) + getshi(4, 7) + getshi(5, 6) << endl;
cout << getxu(1, 10) + getxu(2, 9) + getxu(3, 8) + getxu(4, 7) + getxu(5, 6) << endl;
cout << getshi(2, 10) + getshi(3, 9) + getshi(4, 8) + getshi(5, 7) << endl;
cout << getxu(2, 10) + getxu(3, 9) + getxu(4, 8) + getxu(5, 7) << endl;
cout << getshi(3, 10) + getshi(4, 9) + getshi(5, 8) << endl;
cout << getxu(3, 10) + getxu(4, 9) + getxu(5, 8) << endl;
cout << getshi(4, 10) + getshi(5, 9) << endl;
cout << getxu(4, 10) + getxu(5, 9) << endl;
cout << getshi(5, 10) << endl;
cout << getxu(5, 10) << endl;
return 0;
}