Problem 11
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 20 + 10;
int work(int n) {
int arr[N][N];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
scanf("%d", &arr[i][j]);
}
}
int ans = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(i + 4 <= n) {
ans = max(ans, arr[i][j]*arr[i+1][j]*arr[i+2][j]*arr[i+3][j]);
}
if(j + 4 <= n) {
ans = max(ans, arr[i][j]*arr[i][j+1]*arr[i][j+2]*arr[i][j+3]);
}
if(i + 4 <= n && j + 4 <= n) {
ans = max(ans, arr[i][j]*arr[i+1][j+1]*arr[i+2][j+2]*arr[i+3][j+3]);
}
if(i + 4 <= n && j >= 3) {
ans = max(ans, arr[i][j]*arr[i+1][j-1]*arr[i+2][j-2]*arr[i+3][j-3]);
}
}
}
return ans;
}
int main() {
printf("%d\n", work(20));
return 0;
}
Problem 12 Highly divisible triangular number
思路
计算一个三角形数的约数时,需要用 O ( n ) O(\sqrt{n}) O(n)的算法
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
bool check(int val, int n) {
int cnt = 0;
int m = (int)sqrt(1.0 * val);
for(int i = 1; i <= m; ++i) {
if(val % i == 0) {
cnt += (val/i == i) ? 1 : 2;
}
}
return cnt >= n;
}
int work(int n) {
int val = 0, cnt = 1;
while(true) {
val += cnt;
if(check(val, n)) {
break;
}
++cnt;
}
return val;
}
Problem 13 Large sum
思路
高精度加法求和,取结果的前 10 10 10位
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 100;
void add(char* s1, char* s2, char* sum) {
int len1 = strlen(s1), len2 = strlen(s2);
reverse(s1, s1 + len1);
reverse(s2, s2 + len2);
int len = 0;
int carry = 0;
for(int i = 0; i < len1 || i < len2; i++) {
carry += i<len1 ? s1[i]-'0' : 0;
carry += i<len2 ? s2[i]-'0' : 0;
sum[len++] = carry%10 + '0';
carry /= 10;
}
if(carry) {
sum[len++] = carry + '0';
sum[len] = '\0';
}
reverse(s1, s1 + len1);
reverse(s2, s2 + len2);
reverse(sum, sum + len);
}
LL work() {
char sum[N], s1[N], s2[N];
memset(sum, 0, sizeof(sum));
memset(s2, 0, sizeof(s2));
for(int i = 0; i < 100; i++) {
scanf(" %s", s1);
add(s1, s2, sum);
memcpy(s2, sum, sizeof(sum));
}
LL ans = 0;
for(int i = 0; i < 10; i++) {
ans = ans*10 + sum[i]-'0';
}
return ans;
}
int main() {
printf("%lld\n", work());
return 0;
}
Problem 14 Longest Collatz sequence
思路
直接搜索求每个数的序列长度,取序列最长的那个数字,但搜索过程中要把已经搜索过的数字的序列长度缓存起来,可以减少大量的无效搜索。注意搜索过程的中间数值会爆 i n t int int
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int step[N];
int dfs(LL val) {
if(val < N && step[val] != 0) {
return step[val];
}
int temp = dfs((val & 1) ? val*3+1 : val/2) + 1;
if(val < N) {
step[val] = temp;
}
return temp;
}
int work(int n) {
memset(step, 0, sizeof(step));
step[1] = 1;
int ans = 0, maxStep = 0;
for(int i = 1; i <= n; i++) {
int temp = dfs(i);
if(maxStep < temp) {
maxStep = temp;
ans = i;
}
}
return ans;
}
int main() {
printf("%d\n", work(1000000));
return 0;
}
Problem 15 Lattice paths
思路
实际上是 21 ∗ 21 21*21 21∗21的点阵,简单动态规划,状态转移方程: d p [ i ] [ j ] = d p [ i − 1 ] [ j ] + d p [ i ] [ j − 1 ] dp[i][j] = dp[i-1][j] + dp[i][j-1] dp[i][j]=dp[i−1][j]+dp[i][j−1]
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 20 + 10;
LL work(int n) {
LL dp[N][N];
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 && j == 1) {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[n][n];
}
int main() {
printf("%lld\n", work(21));
return 0;
}