加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

用1-6这6个自然数组成一个三角形,并让这个三角形三条边上数字之和相等

(2022-03-14 10:04:53)
分类: 研究-学习
c++方法:
#include
#include
int main()
{
    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;
}
————————————————
版权声明:本文为CSDN博主「努力努力努力再努力」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42252769/article/details/80572861


python方法:
for a in range(1,7):
    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)

可以稍微看出两种语言在具体语法上的区别:
python更为灵活,判断多个条件同时相等,可以连续多个条件一起判断;
而对于不等于,则不能连续使用,否则就乱了,只能两两判断来。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有