欧拉计划–C++编程突破9
Problem 17
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.
问题17
如果数字1到5用文字写出来:一,二,三,四,五,那么一共用了3个3,5,4=19个字母。 如果从1到1000(1000)的所有数字都用单词写出来,将使用多少个字母? 注意:不要计算空格或连字符。 例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。 在写数字时使用“和”符合英国的用法。
解题思路:这道题有点像找规律题,英文数字的写法规律是:20以内数字不固定,需要自己计算,然后一百以内需要计算十位数字长度再加上个位数字的字母,计算100到1000的数字时记得加"和"长度三个字母以及"百"长度七个字母,最后有1000长度11个字母即可。
#include <stdio.h>
int GetLetterNum(int i) {
static int LN20[20] = {
0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3,
6, 6, 8, 8, 7, 7, 9, 8, 8
};
static int LN_shi[10] = {
0, 0, 6, 6, 5, 5, 5, 7, 6, 6
};
int temp;
if (i < 20) {
return LN20[i];
} else if (i < 100) {
return LN_shi[i / 10] + LN20[i % 10];
} else if (i < 1000) {
temp = GetLetterNum(i % 100);
if (temp != 0) temp += 3;
return temp + LN20[i / 100] + 7;
} else if (i == 1000) {
return 11;
} else {
return 0;
}
return 0;
}
int main() {
int sum = 0;
for (int i = 1; i<= 1000; ++i) {
sum += GetLetterNum(i);
}
printf("%d\n", sum);
return 0;
}
验证answer = 21124
Problem 18
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
问题18
从下面三角形的顶部开始,移动到下面一行的相邻数字,从上到下的最大总数是23。
3
7 4
2 4 6
8 5 9 3
即3+7+4+9=23。
求下面三角形从上到下的最大总数:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
注意:由于只有16384条路线,所以可以通过尝试每条路线来解决这个问题。 然而,问题67,是一个包含100行的三角形的相同挑战;它不能用蛮力解决,需要一个聪明的方法! ;o)
解题思路:简单的dfs算法以及回溯,每次可以递归直到最后一层,比较大小并进行回溯,这个方法比较暴力,如果应用在67题可能不能够通过。另一种则直接进行比较每一步可以比较相邻的两步取最大值。除此之外,本题建议使用文件进行导入。
#include<iostream>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
#define max_n 20
int val[max_n +5][max_n + 5];
int keep[max_n + 5][max_n + 5];
int dfs(int i, int j, int n) {
if (i + 1 == n) return val[i][j];
if (keep[i][j]) return keep[i][j];
int val1 = dfs(i + 1, j, n);
int val2 = dfs(i + 1, j + 1, n);
return keep[i][j] = (val1 > val2 ? val1 : val2) + val[i][j];
}
int main() {
for (int i = 0; i < max_n; i++) {
for (int j = 0; j <= i; j++) {
cin >> val[i][j];
}
}
memset(keep, 0, sizeof(keep));
cout << dfs(0,0,max_n) << endl;
return 0;
}
#include<iostream>
#include<map>
#include<vector>
using namespace std;
#define max_n 20
int val[max_n + 5][max_n + 5];
int main() {
for (int i = 0; i < max_n; i++) {
for(int j = 0; j <= i; j++) {
cin >> val[i][j];
}
}
for (int i = max_n - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
val[i][j] += max(val[i + 1][j], val[i + 1][j + 1]);
}
}
cout << val[0][0] << endl;
return 0;
}
验证answer = 1074