一、复数乘法【哔哩哔哩2020校园招聘】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/22ea9a6b147f076efc6b912d29b22485.png)
#include <iostream>
#include <string>
using namespace std;
int StrToInt(string& s)
{
int num = 0;
int flag = 1;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == '-')
flag = -1;
else
{
if (s[i] <= '9' && s[i] >= '0')
{
num = num * 10 + (s[i] - '0');
}
}
}
return num * flag;
}
int main()
{
string s1;
string s2;
cin >> s1 >> s2;
string s1First = s1.substr(0, s1.find('+'));
string s1Second = s1.substr(s1.find('+') + 1);
string s2First = s2.substr(0, s2.find('+'));
string s2Second = s2.substr(s2.find('+') + 1);
int s1F = StrToInt(s1First);
int s1S = StrToInt(s1Second);
int s2F = StrToInt(s2First);
int s2S = StrToInt(s2Second);
int sum1 = s1F * s2F - s1S * s2S;
int sum2 = s1F * s2S + s1S * s2F;
cout << sum1 << "+" << sum2 << "i" << endl;
}
二、一年中的第几天【哔哩哔哩2020校园招聘】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/016e61df4bfaf85bc678f6405d7ad163.png)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
int month_days[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
int pos1 = s.find('-');
int pos2 = s.find('-', pos1+1);
int year = stoi(s.substr(0, pos1));
int month = stoi(s.substr(pos1+1, pos2));
int day = stoi(s.substr(pos2+1));
int sum = month_days[month-1] + day;
if((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month > 2)
{
sum += 1;
}
cout << sum << endl;
return 0;
}
三、k个一组翻转链表【哔哩哔哩2020校园招聘】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/868810e28121ac5e7bf1eafef7fc1544.png)
#include <vector>
#include <iostream>
#include <string>
using namespace std;
void reverseV(vector<string>& s, int start, int end)
{
while(start < end)
{
s[start].swap(s[end]);
start++;
end--;
}
}
int main()
{
vector<string> lv;
int k = 0;
string str;
while((cin >> str) && (str!= "#"))
{
lv.push_back(str);
}
cin >> k;
int start = 0;
int count = lv.size() / k;
while(count--)
{
reverseV(lv, start, start + k -1);
start += k;
}
for (int i = 0; i < lv.size(); ++i)
{
if (i != lv.size() - 1)
cout << lv[i] << "->";
else
{
cout << lv[i];
}
}
cout << endl;
return 0;
}
四、递增子序列【瓜子二手车2019秋招】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/50030975810a165ad84107f2eacf2da9.png)
#include <iostream>
#include <vector>
using namespace std;
bool isAdd(vector<int>& v)
{
if(v.size() < 3)
{
return false;
}
int first = v[0];
int second = INT32_MAX;
for(int i = 1; i < v.size(); ++i)
{
if(v[i] < first)
{
first = v[i];
second = INT32_MAX;
}
else if(first < v[i] && v[i] < second)
{
second = v[i];
}
else if(v[i] > second)
{
return true;
}
}
return false;
}
int main()
{
int n = 0;
cin >> n;
vector<int> v;
v.resize(n);
for(auto& e : v)
{
cin >> e;
}
if(true == isAdd(v))
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
return 0;
}
五、硬币划分【瓜子二手车2019秋招】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1d39775503be52d979a9759c9f913749.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ab859ba5649f3a5d1599acc430ba5ae8.png)
#include <iostream>
#include <vector>
using namespace std;
int Sum(vector<int>& coins, int n)
{
int row = coins.size();
vector<vector<int>> v(row + 1, vector<int>(n + 1, 0));
for (int i = 0; i <= row; ++i)
{
v[i][0] = 1;
}
for (int i = 1; i <= row; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (coins[i - 1] > j)
{
v[i][j] = v[i - 1][j];
}
else
{
v[i][j] = (v[i - 1][j] + v[i][j - coins[i - 1]]) % 1000000007;
}
}
}
return v[coins.size()][n];
}
int main()
{
int n = 0;
cin >> n;
vector<int> coins{ 1, 2, 5, 10 };
cout << Sum(coins, n) << endl;
return 0;
}
六、求表达式 f(n)结果末尾0的个数【小红书2019年校园招聘】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6aad9994449a0ad23e039fc4eb50395e.png)
#include <iostream>
using namespace std;
int CalNumber(int n, int key)
{
int cnt = 0;
while(n % key == 0)
{
++cnt;
n /= key;
}
return cnt;
}
int main()
{
int n = 0;
cin >> n;
int count2 = 0;
int count5 = 0;
for (int i = 1; i <= n; ++i)
{
count2 += CalNumber(i, 2) * (n - i + 1);
count5 += CalNumber(i, 5) * (n - i + 1);
}
cout << min(count2, count5) << endl;
return 0;
}
七、字符串压缩算法【小红书2019年校园招聘】
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c98d1c3a35b3f09f69baa0bca6acc567.png)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
int cnt = 0;
for (int i = 0; i < s.size(); ++i)
{
while(i < s.size() - 1&& s[i] == s[i + 1])
{
++cnt;
++i;
}
if(cnt != 0)
{
cout << cnt;
cnt = 0;
}
cout << s[i];
}
return 0;
}