用1-6这6个自然数组成一个三角形,并让这个三角形三条边上数字之和相等
(2022-03-14 10:04:53)分类: 研究-学习 |
c++方法:
int a,b,c,d,e,f;
for(a=1;a<=6;a++)
{
for(b=1;b<=6;b++)
{
for(c=1;c<=6;c++)
{
for(d=1;d<=6;d++)
{
for(e=1;e<=6;e++)
{
for(f=1;f<=6;f++)
{
if( a!=b && a!=c
&& a!=d && a!=e && a!=f
&& b!=c && b!=d && b!=e
&& b!=f
&& c!=d &&
c!=e && c!=f
&&
d!=e && d!=f
&& e!=f)
{
if(a+b+c==a+d+e && c+e+f==a+b+c)
{
printf("%d+%d+%d ",a,b,c);
printf("%d+%d+%d ",a,d,e);
printf("%d+%d+%d \n",c,e,f);
}
}
}
}
}
}
}
}
system("pause");
return 0;
for b in
range(1,7):
for c in range(1,7):
for d in
range(1,7):
for e in range(1,7):
for f in range(1,7):
#if
a!=b!=c!=d!=e!=f:
if
(a!=b)and
(a!=c)and(a!=d)and(a!=e)and(a!=f)and(b!=c)and(b!=d)
and(b!=e)and(b!=f)and(c!=d)and(c!=e)and(c!=f)and(d!=e)and(d!=f)and(e!=f):
if ((a+b+c)==(c+d+e)==(e+f+a)):
print(a,b,c,d,e,f)
#include
#include
int main()
{
}
————————————————
版权声明:本文为CSDN博主「努力努力努力再努力」的原创文章,遵循CC 4.0
BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42252769/article/details/80572861
python方法:
for a in range(1,7):
可以稍微看出两种语言在具体语法上的区别:
python更为灵活,判断多个条件同时相等,可以连续多个条件一起判断;
而对于不等于,则不能连续使用,否则就乱了,只能两两判断来。
后一篇:推理题目编程