问:用for语句怎么找出n以内的勾股数。

function num = ggs(n);
num = n;
a = [];
b = [];
c = [];
for i = 1:num
for j = 1:num
for k = 1:num
if i^2+j^2==k^2
a = [a i];
b = [b i];
c = [c k];
end
end
end
end
disp(['勾数有',num2str(a),'b=',num2str(b),'c=',num2str(c)])
gss(10)
a=3 4 6 8b=3 4 6 8c=5 5 10 10
输入数值只能出现这样的答案,我想abc能一起出现比如[a b c] = [3 4 5],请问该怎么改啊?

 Accepted Answer

vmmshm
vmmshm on 21 May 2023

0 votes

仅供参考
ggs(10)
function num = ggs(n)
num = n;
m=0;
for i = 1:num
    for j = 1:num
        for k = 1:num
            if i^2+j^2==k^2
                m=m+1;
                tri(m,:)=[i,j,k];
            end
        end
    end
end
disp('勾数有')
disp(tri)
end

More Answers (0)

Categories

Find more on 循环及条件语句 in Help Center and File Exchange

Asked:

on 21 May 2023

Answered:

on 21 May 2023

Community Treasure Hunt

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

Start Hunting!