💥前言
刷刷刷
😉解题报告
💥组队
☘️ 题目描述☘️
🤔一、思路:
(1)看图手算
😎二、代码:
#include <iostream>
using namespace std;
int main()
{
cout << 98 + 99 + 98 + 97 + 98;
return 0;
}
💥不同子串
☘️ 题目描述☘️
🤔一、思路:
(1)用哈希表进行存储键值对 即标记分割的后的字串, s u b s t r substr substr函数进行字符串的分割
😎二、代码:
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, int> hx;
int main() {
int ret = 0;
string s = "0100110001010001";
int n = s.size();
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
string sub = s.substr(i, j - i + 1);
if (hx[sub] == 0) ret++;
hx[sub]++;
}
}
cout << ret;
return 0;
}
💥等差数列
☘️ 题目描述☘️
🤔一、思路:
(1)求最短的数列,即求数与数之间的最大公差,而最大公差等同于最大公约数,所以数与数之间的最大公约数即可,最后用等差数列的通项公式即可求得项数,考虑特殊情况差值为0得情况,则项数等同于n
😎二、代码:
#include <bits/stdc++.h>
using namespace std;
int num[1000010];
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> num[i];
sort(num, num + n);
int d = num[1] - num[0];
for (int i = 2; i < n; i++) d = gcd(d, num[i] - num[i - 1]);
if (num[n-1] == num[0]) cout << n;
else
cout << ((num[n-1] - num[0]) / d) + 1;
return 0;
}