十一.方阵中的最大乘积
#include <stdio.h>
#include "test_11.h" //调用头文件
#define max_n 20
int dirc[4][2] = {{0,1},{1,0},{1,1},{1,-1}}; //方向数组
int calc(int x, int y){
int p = 0, max = 0;
for(int k = 0; k < 4; k++){ //循环每一个方向
p = 1;
for(int step = 0; step < 4; step++){ //累乘指定方向上各数据
int x1 = x + dirc[k][0] * step;
int y1 = y + dirc[k][1] * step;
if(x1 < 0 || x1 >= max_n) break;
if(y1 < 0 || y1 >= max_n) break;
p *= arr[x1][y1];
}
if(p > max) max = p;//取四个方向中的最大值
}
return max;
}
int main(){
int max = 0, cur = 0;
for(int i = 0; i < max_n; i++){
for(int j = 0; j < max_n; j++){
cur = calc(i, j); //以每一个坐标为基准值
if(cur > max) max = cur;
}
}
printf("%d", max);
return 0;
}
十二.有很多约数的三角形数
#include <stdio.h>
#define max_n 100000
int prime[max_n + 5] = {0};
int f[max_n + 5] = {0};//f[i]表示i的约数个数
int cnt[max_n + 5] = {0};//cnt[i]表示i的最小素因子的幂次
void is_prime(){
for(int i = 2; i < max_n; i++){
if(!prime[i]) {
prime[++prime[0]] = i;
f[i] = 2; //素数因子个数为2
cnt[i] = 1; //素数的最小素因子幂次为1
}
for(int j = 1; j <= prime[0]; j++){
if(i * prime[j] >= max_n) break;
prime[i * prime[j]] = 1;
if(i % prime[j] == 0) {//i为合数
f[i * prime[j]] = f[i] / (cnt[i] + 1) * (cnt[i] + 2);
cnt[i * prime[j]] = cnt[i] + 1;
break;
}else {//i是素数
f[i * prime[j]] = f[i] * f[prime[j]];
cnt[i * prime[j]] = 1;
}
}
}
return ;
}
int main(){
is_prime();
int n = 1;
int max = 0;
while(1){
if(n % 2 == 0) max = f[n / 2] * f[n + 1];
else max = f[(n + 1) / 2] * f[n];
if(max > 500){
printf("%lld", n * (n + 1) / 2);
break;
}
n++;
}
return 0;
}
十三.大和
#include<stdio.h>
#include<string.h>
#define max_n 100
void trasform(const char *str, int *arr){
memset(arr, 0, sizeof(int) * (max_n + 5));
int le = strlen(str);
arr[0] = le;
for(int i = 0; i < le; i++){
arr[le - i] = str[i] - '0';
}
return ;
}
void addition(int *temp, int *arr){
for(int i = 1; i < temp[0]; i++){
arr[i] += temp[i];
}
for(int i = 1; i < arr[0]; i++){
if(arr[i] < 10) continue;
arr[i + 1] += arr[i] / 10;
arr[i] %= 10;
if(i == arr[0]) arr[0]++;
}
return ;
}
int main(){
char str[max_n + 5] = {0};
int arr[max_n + 5] = {0}, temp[max_n + 5] = {0};
arr[0] = 50;
while(scanf("%s", str) != EOF){
trasform(str, temp);
addition(temp, arr);
}
for(int i = 0; i < 10; i++){
printf("%d", arr[arr[0] - i]);
}
return 0;
}
十四.最长考拉兹序列
#include <stdio.h>
#define max_n 1000000
int arr[max_n + 5] = {0, 1}; //记忆化
int calc(long long x){
if(x <= max_n && arr[x]) return arr[x];
int cur;
if(x % 2 == 0) cur = calc(x / 2) + 1;
else return cur = calc(3 * x + 1) + 1;
if(x <= max_n) arr[x] = cur;
return cur;
}
int main(){
int le = 0, max = 0;
for(int i = 1; i < max_n; i++){
int temp = calc(i);
if(temp > le) {
le = temp;
max = i;
}
}
printf("%d", max);
return 0;
}
十五.网格路径
#include <stdio.h>
int main(){
long long x = 1;
for(int i = 1; i <= 20 ;i++){
x *= (20 + i);
x /= i;
}
printf("%lld", x);
return 0;
}
十六.幂的数字和
#include <stdio.h>
#define max_n 1000
void mul(int *arr){
for(int i = 1; i <= arr[0]; i++){
arr[i] += arr[i];
}
for(int i = 1; i <= arr[0]; i++){
if(arr[i] < 10) continue;
arr[i + 1] += arr[i] / 10;
arr[i] %= 10;
if(i == arr[0]) arr[0]++;
}
return ;
}
int main(){
int arr[max_n + 5] = {0};
int sum = 0;
arr[0] = 1, arr[1] = 1;
for(int i = 1; i <= max_n; i++){
mul(arr);
//printf("%d\n", arr[0]);
}
for(int i = 1; i <= arr[0]; i++){
sum += arr[i];
}
printf("%d", sum);
return 0;
}
十七.整数英文表达的字母计数
#include <stdio.h>
#define max_n 1000
int x1[10] = {0,3,3,5,4,4,3,5,5,4};
int x2[11] = {0,3,6,6,8,8,7,7,9,8,8};
int x3[10] ={0,0,6,6,5,5,5,7,6,6};
int arr[max_n + 5] = {0};
int main(){
int le = 0;
for(int i = 1; i < max_n; i++){
if(i < 10) {
arr[i] = x1[i];
le += arr[i];
}
if(i >= 10 && i < 20) {
arr[i] = x2[i - 9];
le += arr[i];
}
if(i >= 20 && i < 100) {
arr[i] = arr[i % 10] + x3[i / 10];
le += arr[i];
}
if(i >= 100){
arr[i] = arr[i / 100] + 7 + arr[i % 100];
if(arr[i % 100]) arr[i] += 3;
le += arr[i];
}
}
printf("%d", le + 11);
return 0;
}