三角形问题
我正在尝试解决欧拉问题 18 -> http://projecteuler.net/index.php?section=problems&id= 18
我正在尝试用 c++ 来做到这一点(我正在重新学习它,欧拉问题是很好的学习/搜索材料)
#include <iostream>
using namespace std;
long long unsigned countNums(short,short,short array[][15],short,bool,bool);
int main(int argc,char **argv) {
long long unsigned max = 0;
long long unsigned sum;
short piramide[][15] = {{75,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{95,64,0,0,0,0,0,0,0,0,0,0,0,0,0},
{17,47,82,0,0,0,0,0,0,0,0,0,0,0,0},
{18,35,87,10,0,0,0,0,0,0,0,0,0,0,0},
{20,4,82,47,65,0,0,0,0,0,0,0,0,0,0},
{19,1,23,75,3,34,0,0,0,0,0,0,0,0,0},
{88,2,77,73,7,63,67,0,0,0,0,0,0,0,0},
{99,65,4 ,28,6,16,70,92,0,0,0,0,0,0,0},
{41,41,26,56,83,40,80,70,33,0,0,0,0,0,0},
{41,48,72,33,47,32,37,16,94,29,0,0,0,0,0},
{53,71,44,65,25,43,91,52,97,51,14,0,0,0,0},
{70,11,33,28,77,73,17,78,39,68,17,57,0,0,0},
{91,71,52,38,17,14,91,43,58,50,27,29,48,0,0},
{63,66,4,68,89,53,67,30,73,16,69,87,40,31,0},
{4,62,98,27,23,9,70,98,73,93,38,53,60,4,23}};
for (short i = 0;i<15;i++) {
for (short m=0;m<15;m++) {
if (piramide[i][m] == 0)
break;
sum = countNums(i,m,piramide,15,true,true);
if (sum > max)
max = sum;
sum = countNums(i,m,piramide,15,true,false);
if (sum > max)
max = sum;
sum = countNums(i,m,piramide,15,false,true);
if (sum > max)
max = sum;
sum = countNums(i,m,piramide,15,false,false);
if (sum > max)
max = sum;
}
}
cout << max;
return 0;
}
long long unsigned countNums(short start_x,short start_y,short array[][15],short size, bool goright,bool goright2) {
long long unsigned currentSum;
currentSum = array[start_x][start_y];
if (goright) { //go right
if ((start_x + 1) < size)
start_x++;
if ((start_y + 1) < size)
start_y++;
}
else //go down
if ((start_x + 1) < size)
start_x++;
if (goright2) { //still going right
for (short i = start_x, m = start_y;i< size && m < size;i++,m++) {
currentSum += array[i][m];
}
}
else { //still going down
for (short i = start_x;i<size;i++) {
currentSum += array[i][start_y];
}
}
return currentSum;
}
countNums 函数用于向下或对角线移动。 我已经像这样测试了这个函数:
short a = 0;
short b = 0;
cout << countNums(a,b,piramide,15,true,true) << endl;
cout << countNums(a,b,piramide,15,true,false) << endl;
cout << countNums(a,b,piramide,15,false,true) << endl;
cout << countNums(a,b,piramide,15,false,false) << endl;
return 0;
它确实有效(我还稍微更改了该函数,以便它可以打印它正在经历的每个数字)
但我仍然没有得到正确的结果。 它向下向右移动,并且仍然向右移动(右侧的相邻数字),向下并继续向下(左侧的相邻数字)。 我在这里做错了什么,有人吗?
Alastair:很简单
long long unsigned countNums(short start_x,short start_y,short array[][15],short size, bool goright,bool goright2);
start_x 和 start_y 是数组的坐标 array 是对数组的引用 size 只是数组的大小(始终为 15) goright 是知道我是要向下向右还是只是向下 goright2是知道我是继续往下走还是往左走
I am trying to resolve Euler Problem 18 -> http://projecteuler.net/index.php?section=problems&id=18
I am trying to do this with c++ (I am relearning it and euler problems make for good learning/searching material)
#include <iostream>
using namespace std;
long long unsigned countNums(short,short,short array[][15],short,bool,bool);
int main(int argc,char **argv) {
long long unsigned max = 0;
long long unsigned sum;
short piramide[][15] = {{75,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{95,64,0,0,0,0,0,0,0,0,0,0,0,0,0},
{17,47,82,0,0,0,0,0,0,0,0,0,0,0,0},
{18,35,87,10,0,0,0,0,0,0,0,0,0,0,0},
{20,4,82,47,65,0,0,0,0,0,0,0,0,0,0},
{19,1,23,75,3,34,0,0,0,0,0,0,0,0,0},
{88,2,77,73,7,63,67,0,0,0,0,0,0,0,0},
{99,65,4 ,28,6,16,70,92,0,0,0,0,0,0,0},
{41,41,26,56,83,40,80,70,33,0,0,0,0,0,0},
{41,48,72,33,47,32,37,16,94,29,0,0,0,0,0},
{53,71,44,65,25,43,91,52,97,51,14,0,0,0,0},
{70,11,33,28,77,73,17,78,39,68,17,57,0,0,0},
{91,71,52,38,17,14,91,43,58,50,27,29,48,0,0},
{63,66,4,68,89,53,67,30,73,16,69,87,40,31,0},
{4,62,98,27,23,9,70,98,73,93,38,53,60,4,23}};
for (short i = 0;i<15;i++) {
for (short m=0;m<15;m++) {
if (piramide[i][m] == 0)
break;
sum = countNums(i,m,piramide,15,true,true);
if (sum > max)
max = sum;
sum = countNums(i,m,piramide,15,true,false);
if (sum > max)
max = sum;
sum = countNums(i,m,piramide,15,false,true);
if (sum > max)
max = sum;
sum = countNums(i,m,piramide,15,false,false);
if (sum > max)
max = sum;
}
}
cout << max;
return 0;
}
long long unsigned countNums(short start_x,short start_y,short array[][15],short size, bool goright,bool goright2) {
long long unsigned currentSum;
currentSum = array[start_x][start_y];
if (goright) { //go right
if ((start_x + 1) < size)
start_x++;
if ((start_y + 1) < size)
start_y++;
}
else //go down
if ((start_x + 1) < size)
start_x++;
if (goright2) { //still going right
for (short i = start_x, m = start_y;i< size && m < size;i++,m++) {
currentSum += array[i][m];
}
}
else { //still going down
for (short i = start_x;i<size;i++) {
currentSum += array[i][start_y];
}
}
return currentSum;
}
The countNums function is used to go either down or diagonally.
I have tested this function like so:
short a = 0;
short b = 0;
cout << countNums(a,b,piramide,15,true,true) << endl;
cout << countNums(a,b,piramide,15,true,false) << endl;
cout << countNums(a,b,piramide,15,false,true) << endl;
cout << countNums(a,b,piramide,15,false,false) << endl;
return 0;
And it does work (I also changed the function a little so it would print every number it was going through)
But I still don't get the right result. This goes down and to the right and still goes right (adjacent numbers to the right), goes down and keeps going down (adjacent number to the left).
What am I doing wrong here, anyone?
Alastair: It's simple
long long unsigned countNums(short start_x,short start_y,short array[][15],short size, bool goright,bool goright2);
start_x and start_y are the coords of the array
array is the reference to the array
size is just the size of the array (it's always 15)
goright is to know if I am going to go down and right or just down
goright2 is to know if I am going to continue to go down or going left
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,首先,我有点不清楚你认为问题是什么。 我根本无法解析倒数第二句话...
其次,您可能想在这里重新考虑您的设计。 考虑执行单个离散任务并且不与应用程序的其余部分交织在一起的函数(即阅读“紧密耦合和松散绑定”)。 对我来说,这里的一个很大的警告是存在一个过于通用的函数名称
countNums
和一长串参数。我认为如果你把问题分成更小、更容易理解的部分,你可能会发现问题更容易找到。 我知道我在这里要采取的方法,但我假设练习的全部目的是帮助您练习编程技能,所以我就这样吧……
Ok so first off, I'm a little unclear as to what you think the problem is. I can't parse that second-last sentence at all...
Secondly you might want to re-think your design here. Think about functions that perform a single discrete task and are not intertwined with the rest of the application (ie read up on "tightly coupled and loosely bound"). For me a big warning bell here is the presence of a overly-generic function name
countNums
with a long list of arguments.I think if you were to divide the problem into smaller, more easily comprehensible, chunks you might find the problems a lot easier to find. I know the approach that I would take here but I'm assuming the whole point of the exercise is to help you practice your programming skills, so I'll just leave it at that...
我已经解决了这个问题。 鉴于欧拉计划的本质是自己解决问题(“复印已解决的填字游戏并不意味着您解决了它”)并且我不想为其他人毁掉这个问题,我真正能说的是您的解决方案看起来过于复杂。
但是,您可以在阅读文件时解决此问题。 这样解决问题 #69 就会变得轻而易举!
祝你好运!
I've solved this problem. Given the nature of Project Euler is to solve the problem on your own ("photocopying a solved crossword puzzle doesn't mean you solved it") and that I don't want to ruin this for someone else, all I can really say is that your solution looks overly complex.
You can, however, solve this problem as you're reading the file. Solve it this way and problem #69 will be a breeze!
Good luck!
我对这个问题有点困惑..
我首先清理你的代码。
现在我读了这个问题,但我不确定它是否符合您的要求。
由于这不是您想要的,我建议首先对答案进行伪编码。
忘记代码了.. sudo 代码中的答案是什么。
哦,还有对所有神圣事物的热爱。 不要在 for 循环中放置多个初始值设定项。 我知道我会因此受到批评,但这很混乱而且真的没有必要。
您可能会考虑使用递归函数。 似乎非常适合这个问题。
I'm a little confused by the problem..
I'd start by cleaning up your code.
Now I read the question and I'm not sure it's doing what you want.
Since this isn't what you want I'd suggest psudo-code the answer first.
Forget the code.. What is the answer in sudo code.
Oh and for the love of all that is holy. Don't put more then one initializer in the for loop. I know I'm going to get flak for that, but it's messy and really not needed.
Something you might consider is a recusive function. Seems ideal for this problem.
main 函数检查条目是否不为零,但它调用的另一个函数不会检查该条目,即使它再次更改索引也是如此。 我不知道,我真的不明白你想做什么。
我看到数组大小为 15,有 15 个元素,声明的索引是否也从 0 开始? 让我检查一下。 只是确保,声明了大小但基于 0 进行访问。很好。
为什么在一处使用嵌套的 for 语句,而稍后使用复合条件的 for 语句? 最好检查
&&
的优先级是否高于<
。 第 8 组击败了第 13 组此处。 增量运算符不在一个语句中多次使用,这很好。听起来你以前用另一种语言做过这个问题,你能对此也做一下跟踪并找到你的新程序首先不同的地方吗?
其他人说得对,这个问题令人困惑。 我首先会尝试解决问题,即在另一个矩阵中建立子分数,如果金字塔从该点开始并从底行到顶部建立,则给出最高分。
The main function checks that an entry isn't zero, but the other function it calls doesn't check for that even though it changes the index again. I don't know, I don't really understand what you are trying to do.
I see array size 15 for 15 elements, would the index for declaration also start at 0? Let me check for a sec. Just making sure, declared with size but accessed based off 0. Good.
Why did you use a nested for statements one place but a compunded-condition for statement later on? Better check that the
&&
doesn't have higher precedence than the<
. Group 8 beats out group 13 here. Increment operator not used multiple times in a statement, that's good.It sounds like you've done this problem before in another language, could you do a trace on that too and find the place your new program first differs?
The other guys are right, the question is confusing. I would first try to solve the problem buy building up subscores in another matrix giving the highest score if the pyramid started at that point and build it up from the bottom row to the top.