科大讯飞2024届秋招研发类笔试试卷(六)前端-编程
1模拟即可
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
int res = 0;
for (auto num : arr)
{
int cur = 0;
string s = to_string(num);
for (int i = 0; i < s.size(); i++)
{
if (s[i] != '0')
{
cur++;
}
}
res += cur;
}
cout << res;
}
// 64 位输出请用 printf("%lld")
2直接左上角一行一行走
#include <bits/stdc++.h>
using namespace std;
int main()
{
// 不需要回到原点,直接从1 1位置一行一行的走就可以了
int n, m;
cin >> n >> m;
string res = "", dd = "", aa = "";
for (int j = 0; j < m - 1; j++)
dd += "D";
for (int j = 0; j < m - 1; j++)
aa += "A";
for (int i = 0; i < n; i++)
{
if (i % 2)
{
res += aa;
}
else
{
res += dd;
}
if (i != n - 1)
res += "S";
}
cout << "1 1" << endl;
cout << res << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
3找规律,删除的方法是 每一轮隔一个删一个(保证次数最小),因此考虑2的幂次,保留的字符是a,需要删的字符是b
k 要删的字符串
1 a
2 aa
3 aaaa (aaaa=>aa=>a=>"")
4 aaaaaaaa (aaaaaaaa=>aaaa=>aa=>a=>"")
即要删的字符串是 2^(k-1)个,然后补上a即可
#include <bits/stdc++.h>
using namespace std;
// 不相邻怎么理解
// 一轮一轮删,每轮删一半
// 需要删的字符串长度是 2^(k-1)个字符串
int main()
{
int n, k;
cin >> n >> k;
int cnt = pow(2, k - 1);
// 测试边缘case :直接-1 是10%数据,也得满足
if (n <= cnt * 2)
{
cout << -1;
return 0;
}
int rest = n - cnt;
for (int i = 0; i < rest; i++)
cout << 'a';
for (int i = 0; i < cnt; i++)
cout << 'b';
return 0;
}
查看7道真题和解析