2022暑期实习-阿里笔试
选择题
单选6道,不定项选择6道,涵盖了操作系统、计算机网络、数据库、数据结构、linux这些范围。
编程题
三道。。。结果我血崩,只做出来半道。
第一道
20000长度的16进制字符串,统计二进制1的个数 16进制 每4位二进制 一个16进制位
我忘了16进制转2进制的方法,先转的10进制,超出范围了。
思路:16进制转2进制,16进制每一位都相当于对应4位二进制,所以就直接每位转成二进制,然后累加1的个数。
#include <iostream>
#include <string>
#include <stack>
#include <unordered_map>
#include <map>
#include <algorithm>
using namespace std;
int toNum(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
if (ch >= 'a' && ch <= 'f') {
return 10 + (ch - 'a');
}
return -1;
}
unordered_map<char, int> ChToOnes;
int main() {
string dic = "0123456789abcdef";
for (int i = 0; i < dic.length(); i++) {
int n = toNum(dic[i]);
int cnt = 0;
while (n != 0) {
if (n % 2 == 1) {
cnt++;
}
n = n >> 1;
}
ChToOnes[dic[i]] = cnt;
}
string s = "";
cin >> s;
long n = 0;
//int bitIdx = 0;
int pow = 1;
int cnt = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s[i] == 'x') {
break;
}
cnt += ChToOnes[s[i]];
}
cout << cnt;
return 0;
}
第二道
0100 1010 1为聚光灯,0为人 上下左右四个方向照,照到人就得分 如上得9分
本想着采用dfs,暴力捡点分,结果出问题了,没调好,哎。。。
思路: 构造两个数组分别横着、竖着存聚光灯 计算的时候两个相邻的聚光灯相减即可
#include <iostream>
#include <string>
#include <stack>
#include <unordered_map>
#include <map>
#include <algorithm>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> LightMap(n, vector<int>(m));
vector<vector<int>> SpotLight_x(n, vector<int>()); // 记录1的横坐标的数组
vector<vector<int>> SpotLight_y(m, vector<int>()); // 记录1的纵坐标的数组
// 填充数组
for (int i = 0; i < n; i++) {
for (int c = 0; c < m; c++) {
cin >> LightMap[i][c];
if (LightMap[i][c] == 1) {
SpotLight_x[i].push_back(c);
SpotLight_y[c].push_back(i);
}
}
}
int sum = 0;
// 相邻坐标之间相减
for (int i = 0; i < SpotLight_x.size(); i++) {
for (int c = 0; c < SpotLight_x[i].size(); c++) {
int left = 0;
if (c != 0) {
left = SpotLight_x[i][c] - SpotLight_x[i][c - 1] - 1;
}
else {
left = SpotLight_x[i][c];
}
int right = 0;
if (c != SpotLight_x[i].size() - 1) {
right = SpotLight_x[i][c + 1] - SpotLight_x[i][c] - 1;
}
else {
right = m - SpotLight_x[i][c] - 1;
}
sum += left + right;
}
}
// 相邻坐标之间相减
for (int i = 0; i < SpotLight_y.size(); i++) {
for (int c = 0; c < SpotLight_y[i].size(); c++) {
int top = 0;
if (c != 0) {
top = SpotLight_y[i][c] - SpotLight_y[i][c - 1] - 1;
}
else {
top = SpotLight_y[i][c];
}
int bottom = 0;
if (c != SpotLight_y[i].size() - 1) {
bottom = SpotLight_y[i][c + 1] - SpotLight_y[i][c] - 1;
}
else {
bottom = n - SpotLight_y[i][c] - 1;
}
sum += top + bottom;
}
}
cout << sum;
return 0;
}