本次春招测试题总共5道题,只有编程,每题20分,总共100分。
题目在剑指和leetcode都有原型题目,之后在分析的时候,也会给出参考。
1. 数字置换
解析:“第i个整数表示将数字i替换为数字ai”,这直接可以采用哈希表来存储,相关数据结构的用法和技巧,在《数据结构与算法》的课中数组一章也有详细讲。
关键在于如何提高算法时间效率和降低空间效率。
#include <iostream>
using namespace std;
int* fun(long long a,int *b)
{
int length = 0;
//cout<<a;
if(a == 0) return NULL;
if(a<0)
{
cout<<"-";
a = 0-a;
}
long long temp = a;
while(temp)
{
temp /= 10;
length++;
}
//cout<<length<<endl;;
int* arr = new int[length];
int l = length;
for(int i = l; i>=0; i--)
{
arr[i] = a%10;
//cout<<arr[i];
a /= 10;
}
for(int j = 1; j<=length; j++)
{
arr[j] = b[arr[j]-1];
cout<<arr[j];
}
cout<<endl;
return arr;
delete[] arr;
}
void test1()
{
long long a = 1234567;
int b[10] = {9,8,7,6,5,4,3,2,1};
fun(a,b);
}
void test2()
{
long long a = -12;
int b[10] = {2,3,7,6,5,4,3,2,1};
fun(a,b);
}
void test3()
{
long long a = 73598793378342493;
int b[10] = {1,3,6,1,6,8,9,1,3};
fun(a,b);
}
void test4()
{
long long a = 0;
int b[10] = {1,3,6,1,6,8,9,1,3};
fun(a,b);
}
int main()
{
// long long a = -12;
// //cin>>a;
// int b[10] = {0,0,0};
//
// int i = 0;
// while(cin>>b[i])
// i++;
//
// int *p = fun(a,b);
//cout<<p;
test1();
test2();
test3();
test4();
return 0;
}
2.同心圆
递归/循环: 剑指10:斐波那契数列
课程对应讲解:https://edu.csdn.net/course/detail/28681 第二章第一节。
#include <iostream>
#include <math.h>
using namespace std;
double AreaOfCircle(int r2,int r1)
{
return M_PI*(r2*r2 - r1*r1);
}
double AreaOfRing(int n, int* r)
{
//(((nums&1)==0)?"偶数":"奇数")
double result = 0.0;
if(n == 1)
return result = M_PI*r[0]*r[0];
if(n == 2)
return result = AreaOfCircle(r[1],r[0]);
if((n&1)==0) //the number of r is even
{
int i = 1;
double temp = 0;
while(2*i <= n)
{
temp = AreaOfCircle(r[2*i-1],r[2*i-2]);
result += temp;
i++;
}
}
else //the number of r is odd
{
int i = 0;
double temp = M_PI*r[0]*r[0];
result = 0;
while(2*i+1 <= n)
{
temp = AreaOfCircle(r[2*i+1],r[2*i]);
result += temp;
i++;
}
}
return result>0?result:-result;
}
void test1()
{
int n = 1;
int r[] = {3};
printf("test1: %.5f\n",AreaOfRing(n,r));
}
void test2()
{
int n = 8;
int r[] = {1,2,3,4,5,6,7,8};
printf("test2: %.5f\n",AreaOfRing(n,r));
}
void test3()
{
int n = 5;
int r[] = {1,2,3,4,5};
printf("test3: %.5f\n",AreaOfRing(n,r));
}
int main()
{
test1();
test2();
test3();
return 0;
}
3.套娃前缀和
4.小仓的射击练习3
5. 子序列计数
推荐阅读:
[1] 数据结构与算法 | 你知道快速排序,那你知道它的衍生应用吗?Partition函数
[2] 数据结构与算法 | 数据结构中到底有多少种“树”?一文告诉你
[3] 数据结构与算法 | 二分查找:剑指offer53 在排序数组中查找数字
[4] 2020字节跳动秋招笔试题解析与代码分享(持续更新中)
关注微信公众号:迈微电子研发社,回复获取更多精彩内容。
知识星球:社群旨在分享AI算法岗的秋招/春招准备攻略(含刷题)、面经和内推机会、学习路线、知识题库等。