方阵中的最大乘积
#include <bits/stdc++.h>
using namespace std;
int a[30][30], maxx;
int dx[4] = {0, 1, 1, -1};
int dy[4] = {1, 0, 1, 1};
int main() {
for (int i = 4; i <= 23; i ++) {
for (int j = 1; j <= 20; j ++) {
cin >> a[i][j];
}
}
for (int i = 4; i <= 23; i ++) {
for (int j = 1; j <= 20; j ++) {
for (int k = 0; k < 4; k ++) {
int num = 1;
for (int l = 0; l < 4; l ++) {
num *= a[i + dx[k] * l][j + dy[k] * l];
}
maxx = max(maxx, num);
}
}
}
cout << maxx;
return 0;
}
多约数的三角形数
#include <iostream>
using namespace std;
int f(int x) {
int res = 1;
for (int i = 2; i * i <= x; i ++) {
int p = 0;
while (x % i == 0) {
x /= i;
p ++;
}
res *= (p + 1);
}
if (x > 1) res *= 2;
return res;
}
int main() {
for (int i = 1; ; i ++) {
int x = i * (i + 1) / 2;
if (f(x) > 500) {
cout << x;
return 0;
}
}
return 0;
}
大整数的和
#include <bits/stdc++.h>
using namespace std;
vector <int> rd() {
vector <int> res;
string s;
cin >> s;
for (int i = s.size() - 1; i >= 0; i --) res.push_back(s[i] - '0');
return res;
}
vector <int> add(vector <int> a, vector <int> b) {
vector <int> res;
int t = 0, len = max(a.size(), b.size());
for (int i = 0; i < len; i ++) {
if (i < a.size()) t += a[i];
if (i < b.size()) t += b[i];
res.push_back(t % 10);
t /= 10;
}
if (t) res.push_back(t);
return res;
}
int main() {
vector <int> a, s;
s.push_back(0);
for (int i = 1; i <= 100; i ++) {
a = rd();
s = add(s, a);
}
for (int i = s.size() - 1; i >= s.size() - 10; i --) cout << s[i];
return 0;
}
最长考拉兹序列
#include <bits/stdc++.h>
using namespace std;
int main() {
int maxx = 0, ans;
for (int i = 1; i <= 1000000; i ++) {
long long x = i, cnt = 1;
while (x > 1) {
if (x % 2 == 0) x /= 2;
else x = x * 3 + 1;
cnt ++;
}
if (cnt > maxx) {
maxx = cnt;
ans = i;
}
}
cout << ans;
return 0;
}
网格路径
#include <bits/stdc++.h>
using namespace std;
long long f[30];
int main() {
int n = 21, m = 21;
f[1] = 1;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
f[j] += f[j - 1];
}
}
cout << f[m];
return 0;
}
幂的数字和
#include <bits/stdc++.h>
using namespace std;
vector <int> mul(vector <int> a, int x) {
vector <int> res;
int t = 0;
for (int i = 0; i < a.size(); i ++) {
t += a[i] * x;
res.push_back(t % 10);
t /= 10;
}
while (t) res.push_back(t % 10), t /= 10;
return res;
}
int main() {
vector <int> a;
a.push_back(1);
for (int i = 1; i <= 1000; i ++) a = mul(a, 2);
int ans = 0;
for (int i = 0; i < a.size(); i ++) ans += a[i];
cout << ans;
return 0;
}
用英文写出1到1000的所有数字需要多少个字母?
#include <bits/stdc++.h>
using namespace std;
int a[10] = {0, 3, 3, 5, 4, 4, 3, 5, 5, 4};
int b[10] = {3, 6, 6, 8, 8, 7, 7, 9, 8, 8};
int c[10] = {0, 0, 6, 6, 5, 5, 5, 7, 6, 6};
int main() {
int ans = 0, t;
for (int i = 1; i < 1000; i ++) {
t = i / 100;
if (t) {
ans += a[t] + 7;
if (i % 100 != 0) {
ans += 3;
}
}
t = i / 10 % 10;
if (t == 0) {
ans += a[i % 10];
}
else if (t == 1) {
ans += b[i % 10];
}
else {
ans += c[t];
ans += a[i % 10];
}
}
ans += 11;
cout << ans;
return 0;
}
最大路径和 I
#include <bits/stdc++.h>
using namespace std;
int a[20][20], f[20];
int main() {
for (int i = 1; i <= 15; i ++) {
for (int j = 1; j <= i; j ++) {
cin >> a[i][j];
}
}
for (int i = 15; i >= 1; i --) {
for (int j = 1; j <= 15; j ++) {
f[j] = max(f[j], f[j + 1]) + a[i][j];
}
}
cout << f[1];
return 0;
}
周日计数
#include <bits/stdc++.h>
using namespace std;
int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
int x = 2, cnt = 0;
for (int y = 1901; y <= 2000; y ++) {
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) a[2] = 29;
else a[2] = 28;
for (int m = 1; m <= 12; m ++) {
if (x == 0) cnt ++;
x = (x + a[m]) % 7;
}
}
cout << cnt;
return 0;
}
阶乘数字和
#include <bits/stdc++.h>
using namespace std;
vector <int> mul(vector <int> a, int x) {
vector <int> res;
int t = 0;
for (int i = 0; i < a.size(); i ++) {
t += a[i] * x;
res.push_back(t % 10);
t /= 10;
}
while (t) res.push_back(t % 10), t /= 10;
return res;
}
int main() {
vector <int> a;
a.push_back(1);
for (int i = 1; i <= 100; i ++) a = mul(a, i);
int ans = 0;
for (int i = 0; i < a.size(); i ++) ans += a[i];
cout << ans;
return 0;
}
|