讲解视频在b站。
试题B:年号字串
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> number;
number.push_back(0);
for (int i = 0; i < n - 1; i ++ )
{
number[0] ++ ;
for (int j = 0; number[j] == 26; j ++ )
{
number[j] = 0;
if (j + 1 == number.size()) number.push_back(0);
else number[j + 1] ++ ;
}
}
for (int i = number.size() - 1; i >= 0; i -- ) cout << char('A' + number[i]);
cout << endl;
}
试题G:完全二叉树的值
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long LL;
int main()
{
int n;
cin >> n;
LL maxv = INT_MIN, maxv_d = 0;
for (int i = 0, length = 1, depth = 1; i < n; depth ++, length *= 2)
{
LL s = 0;
for (int j = 0; j < length && i < n; j ++, i ++ )
{
int x;
cin >> x;
s += x;
}
if (s > maxv)
{
maxv = s;
maxv_d = depth;
}
}
cout << maxv_d << endl;
return 0;
}
试题H:等差数列
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <limits.h>
using namespace std;
const int N = 100010;
int n;
int a[N];
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
sort(a + 1, a + 1 + n);
for (int i = 2; i <= n; i ++ ) a[i] -= a[1];
int d = a[2];
for (int i = 3; i <= n; i ++ ) d = gcd(d, a[i]);
if (d) cout << a[n] / d + 1 << endl;
else cout << n << endl;
return 0;
}