Project Euler: Problem 11

3 views (last 30 days)
Nguyen Huy
Nguyen Huy on 14 Jul 2021
Edited: John D'Errico on 16 Jul 2021
% the problem is inspired by Project Euler 11
%It pass 2 test but failed in test 3,i dont know why
%here is my code
functiony = PE11(A,k)
[b c]=size(A)
m = 0;
dx = [1, 0, 1,-1];
dy = [0, 1, 1,-1];
%check product on horizontal
fory=1:b
forx=1:c-k
p=1;
fori = 0:k-1
p=p*A(y+i*dy(1),x+i*dx(1));
end
m=max(p,m);
end
end
%check product on vertical
fory=1:b-k
forx=1:c
p=1;
fori = 0:k-1
p=p*A(y+i*dy(2),x+i*dx(2));
end
m=max(p,m);
end
end
%check product on diagonal form right to left
fory=1:b-k
forx=1:c-k
p=1;
fori = 0:k-1
p=p*A(y+i*dy(3),x+i*dx(3));
end
m=max(p,m);
end
end
%check product on diagonal form left to right
fory=b:-1:k
forx=c:-1:k
p=1;
fori = 0:k-1
p = p*a(y+i*dy(4),x+i*dx(4));
end
m=max(p,m);
end
end
y=m;
end
1 Comment
John D'Errico
John D'Errico on 16 Jul 2021
Don't add an answer pleading for help. That is not an answer. (I've deleted your non-answer.)

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 16 Jul 2021
Edited:John D'Errico on 16 Jul 2021
As far as why it failed test case #3, perhaps you need to write better, more efficient code. The code you wrote will be a bit of a CPU hog, and I recall the Cody problems time out for excessively slow solutions. (I've not looked carefully at your code. But even if it does work, it will get a really poor Cody score. The scoring algorithm for Cody hates massively nested loops when compared to more elegant solutions.)
由于该科迪问题的第三个测试案例明显比其他问题大得多,并且您声称您的代码适用于案例1和2,因此我假设您的代码在冬季的Mollasses速度很慢。
您的代码可以用于解决真正的PE问题吗?没门!因此,您可能需要找到不使用大量嵌套环的解决方案。我可以想到至少一个,好的,也许是两个解决方案。金宝搏官方网站(虽然我不会告诉您如何解决一个让您想到的问题,只是告诉您,大量嵌套的循环是一个坏主意,这可能就足够了。)

More Answers (0)

下载188bet金宝搏


发布

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!