跑步训练
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int a = 600 / 60, b = 300 / 60;
int t = 0, m = 10000;
int ans = 0;
while (m >= a * 60)
{
m -= a * 60;
m += b * 60;
ans += 120;
}
if (m)
{
ans += m / a;
cout << ans << endl;
}
return 0;
}
纪念日
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool is_leap(int y)
{
return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}
int main()
{
int cnt = 0;
for (int i = 1922; i <= 2020; i ++ )
{
cnt += 365 + is_leap(i);
}
cout << (cnt - 22) * 60 * 24 << endl;
return 0;
}
合并检测
这题概率论
在这里插入代码片
REPEAT程序
#include<iostream>
using namespace std;
int main()
{
int ans = 0;
for(int i = 0; i < 2; i++)
{
ans += 4;
for(int j = 0; j < 5; j++)
{
for(int k = 0; k < 6; k++)
{
ans += 5;
}
ans += 7;
}
ans += 8;
}
ans += 9;
cout << ans <<endl;
return 0;
}
整除序列
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin >> n;
while(n > 0)
{
cout << n << ' ';
n = n / 2;
}
return 0;
}
解码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool check(char c)
{
return c >= '0' && c <= '9';
}
int main()
{
string str;
cin >> str;
string res;
for (int i = 0; i < str.size(); i ++ )
{
if (check(str[i]))
{
int j = i;
while (j < str.size() && check(str[j])) j ++ ;
int cnt = stoi(str.substr(i, j - i)) - 1;
while (cnt -- ) res += str[i - 1];
i = j - 1;
}
else res += str[i];
}
cout << res << endl;
return 0;
}
走方格
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 35;
int n, m;
int f[N][N];
int main()
{
scanf("%d%d", &n, &m);
f[1][1] = 1;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
if (i == 1 && j == 1) continue;
if (i % 2 || j % 2)
{
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
cout << f[n][m] << endl;
return 0;
}
整数拼接
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long LL;
int n, m;
int a[N], s[11][N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for (int i = 0; i < n; i ++ )
{
LL t = a[i] % m;
for (int j = 0; j < 11; j ++ )
{
s[j][t] ++ ;
t = t * 10 % m;
}
}
LL res = 0;
for (int i = 0; i < n; i ++ )
{
LL t = a[i] % m;
int len = to_string(a[i]).size();
res += s[len][(m - t) % m];
LL r = t;
while (len -- ) r = r * 10 % m;
if (r == (m - t) % m) res -- ;
}
printf("%lld\n", res);
return 0;
}
超级胶水
这题其实很坑,本来时想着用DP做,发现数据范围不对。模拟了一下数据发现交换顺序不影响答案,好家伙,玩因的是叭…
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
typedef long long LL;
LL res = 0;
int n;
int main()
{
scanf("%d", &n);
LL last = 0;
for (int i = 1; i <= n; i ++ )
{
int x;
cin >> x;
if (i > 1)
{
res += last * x;
last = last + x;
}
else last = x;
}
printf("%lld\n", res);
return 0;
}
网络分析
不创建新节点写法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110010;
int n, m;
int p[N], d[N];
int find(int x)
{
if (p[x] == x || p[p[x]] == p[x]) return p[x];
int r = find(p[x]);
d[x] += d[p[x]];
p[x] = r;
return r;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i < N; i ++ ) p[i] = i;
int op, a, b;
int k = n;
while (m -- )
{
scanf("%d%d%d", &op, &a, &b);
if (op == 1)
{
a = find(a), b = find(b);
if (a != b)
{
d[a] -= d[b];
p[a] = b;
}
}
else
{
a = find(a);
d[a] += b;
}
}
for (int i = 1; i <= n; i ++ )
if (find(i) == i) printf("%d ", d[i]);
else printf("%d ", d[i] + d[find(i)]);
puts("");
return 0;
}
创建新结点写法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110010;
int n, m;
int p[N], d[N];
int find(int x)
{
if (p[x] == x || p[p[x]] == p[x]) return p[x];
int r = find(p[x]);
d[x] += d[p[x]];
p[x] = r;
return r;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i < N; i ++ ) p[i] = i;
int op, a, b;
int k = n;
while (m -- )
{
scanf("%d%d%d", &op, &a, &b);
if (op == 1)
{
a = find(a), b = find(b);
if (a != b)
{
k ++ ;
p[a] = p[b] = k;
}
}
else
{
a = find(a);
d[a] += b;
}
}
for (int i = 1; i <= n; i ++ )
if (find(i) == i) printf("%d ", d[i]);
else printf("%d ", d[i] + d[find(i)]);
puts("");
return 0;
}