• 给你一个数N,让你求出 这样的组合A,B,C,A,B,C要满足如下条件:A*A + B*B = C*C,A+B+C<=N


    代码
                int n = 100;
                Action
    <intint, Action<int>> myfor = (start, end, callback) => {
                    
    for (int i = start; i < end; i++) {
                        callback(i);
                    }
                };

                myfor(
    1, n, (a) => myfor(1, n, (b) => myfor(1, n, (c) => {
                    
    if (a * a + b * b == c * c)
                        Console.WriteLine(
    "a:{0},b:{1},c:{2},n:{3}", a, b, c, n);
                })));

                
    for (int a = 1; a < n; a++) {
                    
    for (int b = 1; b < n; b++) {
                        
    for (int c = 1; c < n; c++) {
                            
    if (a * a + b * b == c * c)
                                Console.WriteLine(
    "a:{0},b:{1},c:{2},n:{3}", a, b, c, n);
                        }
                    }
                }

                var r 
    = from a in Enumerable.Range(1, n)
                        from b 
    in Enumerable.Range(a, n)
                        from c 
    in Enumerable.Range(b, n)
                        
    where a + b + c < n &&
                        a 
    * a + b * b == c * c
                        select 
    new { A = a, B = b, C = c };
                
    foreach (var item in r) {
                    Console.WriteLine(
    "a:{0},b:{1},c:{2},n:{3}", item.A, item.B, item.C, n);
                }

    给你一个数N,让你求出 这样的组合A,B,C,A,B,C要满足如下条件:A*A + B*B = C*C,A+B+C<=N

    最后一种方法的结果和前两种的结果不一样,不知道为啥。

  • 相关阅读:
    qrcode在手机上不显示的问题
    css 文本溢出省略号
    css解决字段不换行
    vue小程序ref和v-for结合使用得到ref数组的一些问题
    Nginx CORS 跨域资源共享问题
    基于k8s使用helm安装Jenkins
    nginx通过自定义http header 进行服务转发
    基于Kubernetes部署nacos配置中心
    基于Centos 7.8 和Kubeadm部署k8s高可用集群
    Jenkins学习以及配置
  • 原文地址:https://www.cnblogs.com/onlytiancai/p/1717978.html
Copyright © 2020-2023  润新知