This commit is contained in:
HuangHai
2025-08-30 18:35:01 +08:00
commit 1f397eca87
6258 changed files with 1487241 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int main() {
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
cout << a[1] << endl;
return 0;
}

View File

@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
using namespace std;
bool isQm(int x) {
int t = x;
while (t) {
int a = t % 10;
if (a > 0 && x % a > 0) return false;
t /= 10;
}
return true;
}
int main() {
int n, m;
cin >> n >> m;
int cnt = 0;
for (int i = n; i <= m; i++)
if (isQm(i)) {
cout << i << " ";
cnt++;
}
if (cnt == 0) cout << -1;
return 0;
}

View File

@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;
/*
测试样例:
abcdefgh
输出:
aebfcgdh
思考:
行数:n, 列数:m 需要满足 m>=n
(1) 1行, m=length(abcdefgh)=8 n=1 满足m>=n
(2) 2行, m=8/2=4 n=2 满足m>=n
(3) 3行, 8不能被3整除,跳过,尝试继续增大行数试试
(4) 4行, m=8/4=2, n=4 m<n 不满足条件了,break
*/
const int N = 210;
char a[N][N]; // 开一个二维数组用来模拟
int n, m; // 最后是几行几列
int main() {
string s;
cin >> s; // 生成一个n行m列的矩阵
// 1、几行几列是答案?
int len = s.size();
for (int i = 1;; i++) {
if (len % i == 0 && len / i >= i) n = i; // 找到一个合理解,更新之
if (len / i < i) break; // 跑过了,就停下来
}
m = len / n; // 每行多少列
// 2、做一个二维矩阵存进去
for (int i = 0; i < n; i++) // 行
for (int j = 0; j < m; j++) // 列
a[i][j] = s[i * m + j];
// 3、从第一列开始,逐列从上下到输出矩阵中的字符
for (int j = 0; j < m; j++)
for (int i = 0; i < n; i++)
cout << a[i][j];
return 0;
}

View File

@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int f[N]; // DP结果数组 f[i]含义:第i层台阶共有多少种到达的方法数
int n, k;
/*
4 2
5
*/
int main() {
cin >> n >> k;
// 特判K=1
if (k == 1) {
cout << 1 << endl;
exit(0);
}
// 初始化 K>=2
f[1] = 1;
f[2] = 2;
// 状态转移方程
for (int i = 3; i <= n; i++)
for (int j = max(i - k, 1); j <= max(1, i - 1); j++)
f[i] += f[j];
// for (int i = 1; i <= n; i++) cout << f[i] << " ";
cout << f[n] << endl;
return 0;
}

View File

@@ -0,0 +1,66 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int a[N];
int sum, avg, total;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % n) {
cout << -1 << endl;
exit(0);
}
avg = sum / n; // 平均数
/*
算法思路
1、求出平均数avg
2、枚举每个数字:
(1) 如果当前数字比avg大,不断向后面找出比avg小的数,将自己多出的部分传递给这个数=min(超过avg的量,少于avg的量)
如果一下子没用了,就继续向后做同样的操作
(2) 如果当前数字比avg小,不断向后面找出比avg大的数,将自己少出的部分传递给这个数=min(超过avg的量,少于avg的量)
如果一下子没用了,就继续向后做同样的操作
举栗子思考:
3
10 1 1
3
5 1 6
3
1 5 6
*/
for (int i = 1; i <= n; i++) {
int d = a[i] - avg; // 差值,注意:这个差值可能是正的,也可能是负的
total += abs(d);
for (int j = i + 1; j <= n; j++) { // 枚举i的所有后位的小朋友,如果当前i位上的小朋友需要糖果或者多出来糖果时,应该把
// 多出来的糖果分给哪些小朋友,或者,少出来的糖果需要从哪些小朋友那里要过来
// 向后找比avg小的
if (d > 0 && a[j] < avg) {
int x = min(d, avg - a[j]);
a[i] -= x;
a[j] += x;
d -= x;
}
// 向后找比avg大的
if (d < 0 && a[j] > avg) {
int x = min(abs(d), a[j] - avg);
a[i] += x;
a[j] -= x;
d += x;
}
if (d == 0) break;
}
}
cout << total << endl;
return 0;
}

View File

@@ -0,0 +1 @@
<mxfile host="Electron" modified="2024-03-11T07:49:39.890Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/14.9.6 Chrome/89.0.4389.128 Electron/12.0.16 Safari/537.36" etag="tFvaNrIXzMi-P0cMovxI" version="14.9.6" type="device"><diagram id="4J5xsyxzfobqGXuH9jze" name="第 1 页">7Ztbb5swFIB/DY+TwOaWxyRtt5dJ0/rQvU0ouMEdwRE4De2vn2lsCDYRqRUn2nDVKOFgjsP5fDgXiAOXm/prmWyz7yRFuQPctHbgnQOA5wPgNP9u+naQxJAL1iVO+aBO8IjfERe6XLrDKap6AykhOcXbvnBFigKtaE+WlCXZ94c9k7w/6zZZI0XwuEpyVfqEU5pxqRfOuh3fEF5nfOoYRIcdm0QM5mdSZUlK9kcieO/AZUkIPXza1EuUN8YTdjkc93Bib/vFSlTQcw6g7yTbbX4+/UrrGgFwV+OX31+4ltck33Un7DYiyF8cV0XfhEkoqtmEi4xucibw2MeKluQPWpKclExSkIKNXDzjPJdESY7XBdtcsa+MmHzxikqKmbHnfMcGp2kzzWKfYYoet8mqmXPPlhaTlWRXpKg5G7dRTwrKl4sfsG1+Ikwhqk9ayGvtzhYsIhtEyzc2hB/gc1J8qYZ8c99xb5dldoRcAE74Ulu3ijsa7AMH8gk4QIGjsEBFOm9WeWPUPKkqvPrAkZRUFR8BGzYeShVfGDXdkXGCAdsIWYnyhOLXvvohg/EZfhDMJm7JhH0ynmzyiuzKFeIHHfuApGc2oocZbo2ooueDXnvS+kChBcpBACiR8DWJeu6IIsNIA4tUkBhzrrORymvjyl4aKkhFPBTv3mRj4u2DYmQdbjgoQm1/80cUGfa32BIVJGKJxKWioqLIMNKZRXoi0dFGCm6MVFzkz2TKo1uaVNlHePL+bYyKQ4WX8kz3yhjVin9KGOHsQhhlRdfGqPYGRHLqs1fgTLlx40e3TlI93wbAEwEwCDRdLhzRY9rjbKXfXvy8/sUvgJpIZyN6TCNVK/3JIg0k74KeHlK5eaMoMs3U9ge6ajKWUABNpuGIItNM1Q6B6Mb5/D1QIE8l2Qlvn+zYav9UAyfSTXbkMlFRZNjlxGKxTEEc9EnoJjvhiB7TRD9X+P/PROVkJ7pUsqMoMs1U7QJMlSnzUwmFZrID3BFFppmqDwkMJDtTuw0ZRFLWoyY9EF4z6QG2HdAGSK/PRvtZjhCMKDLterZ2FCiisWcwzmY69uiWaab27nLLVPZTT5ep/KSPrMg0U1twtkyjfgtV20+jYESR6UcjbcHZFZyhhEKzPInhiCLTTNV4Cp370Jkvnfk5uavJnLTZFimwA+DDg8v+LpOrQql3M1NzVW+oQSdnOxfLVaEaA/0pcJDiHYhvDkINXJMAITnE0P3564Lw1WgzRRBD9w6uDELtSgYTADGTLk2BOQ5ss/uN2SGyd7/Ug/d/AQ==</diagram></mxfile>

View File

@@ -0,0 +1,16 @@
5
1 2 3 3 2
5
2 3 2 3 1
5
2 3 4 5 2
5
3 2 4 2 5
7
3 2 4 2 5 3 1
0

View File

@@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int a[N];
int st[10];
vector<PII> q;
int res;
int n;
int main() {
#ifndef ONLINE_JUDGE
freopen("6.in", "r", stdin);
#endif
while (cin >> n && n) {
// 各种清空
memset(a, 0, sizeof a);
memset(st, 0, sizeof st);
q.clear();
res = 0;
for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组
for (int i = 1; i <= n; i++) { // 遍历每个位置
if (!st[a[i]]) { // 如果此数字没有被处理过
st[a[i]] = 1; // 标识已处理
for (int j = n; j; j--) // 从后向前找到它的终止位置
if (a[i] == a[j]) {
q.push_back({i, j}); // 记录有效区间
break;
}
}
}
// 从左到右枚举,所以,不再需再进行按左端点排序,现在就是按左端点排序完的
// Q:求一组区间的相交区间个数?如果存在一个相交区间,就多加1个1
res = q.size();
// 如果存在区间相交,则res++
for (int i = 0; i < q.size(); i++)
for (int j = i + 1; j < q.size(); j++)
if (q[i].second > q[j].first && q[j].second > q[i].second) res++;
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
cout << res << endl;
cout << endl;
}
return 0;
}

View File

@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;
/*
测试用例:
5
12321
思路:
1、先看前后是不是存在一样的,这样的优先处理
2、
*/
int cnt = 1;
// 循环写法
void cut(string s) {
while (true) {
int p;
for (p = s.size() - 1; p; p--)
if (s[0] != s[p]) break;
string t;
for (int i = 1; i <= p; i++) t += s[i];
if (t.size())
cnt++, s = t;
else
break;
}
}
int main() {
int n;
string s;
cin >> n >> s;
cut(s);
cout << cnt << endl;
return 0;
}

View File

@@ -0,0 +1,64 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
/*
4
1 2 3 4
*/
const int N = 110;
struct Node {
int num; // 啥数
int left, right; // 左侧是哪个节点,右侧是哪个节点
} a[N];
int st[N];
int n;
LL res;
string finalPath;
void dfs(int r, LL sum, string path) { // u:本轮选择哪个靶子, r:剩余几次机会 sum:已经取得的累加和
if (r == 0) {
if (sum > res) {
res = sum;
finalPath = path;
}
return;
}
for (int u = 1; u <= n; u++) {
if (!st[u]) {
int L = a[u].left, R = a[u].right;
st[u] = 1; // u号靶子被标识为已放倒
a[L].right = R;
a[R].left = L;
// 深搜
dfs(r - 1, sum + a[L].num * a[u].num * a[R].num, path + to_string(u));
// 回溯
a[L].right = u;
a[R].left = u;
st[u] = 0; // u被扶了起来
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].num;
// 左右哨兵
a[0].num = 1;
a[n + 1].num = 1;
// 预处理
for (int i = 1; i <= n; i++) {
a[i].left = i - 1;
a[i].right = i + 1;
}
dfs(n, 0, "");
cout << res << endl;
cout << finalPath << endl;
return 0;
}

View File

@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
bool check(int x) {
int len = to_string(x).size();
int sum = 0, t = x;
while (t) {
int a = t % 10;
sum += pow(a, len);
t /= 10;
}
return x == sum;
}
int main() {
int T;
cin >> T;//times
while (T--) {
int a;
cin >> a;
if (check(a))
cout << 'T' << endl;
else
cout << 'F' << endl;
}
return 0;
}

View File

@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;
int cnt;
bool check(int x) {
string s = to_string(x);
string t = s;
reverse(t.begin(), t.end());
if (s != t) return false;
bool flag = false;
for (int i = 0; i < s.size(); i++)
if (s[i] == '7') flag = true;
if (!flag) return false;
return true;
}
int main() {
int a, b;
cin >> a >> b;
for (int i = a; i <= b; i++)
if (check(i)) cnt++;
cout << cnt << endl;
return 0;
}