• 百钱买鸡鸣


    公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,

    用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。

    不定方程-->

             x+y+z=100,

             5x+3y+z/3=100,

             5x<100 => 0<x<20, 同理  0<y<33,那么z=100-x-y,

    c#代码-->

    for (int x=1; x<30; x++)
                {
                    for (int y=1; y<33; y++)
                    {
                        int z = 100 - x - y;
                        if (5*x+3*y+z*1.0/3.0 == 100)
                        {
                            Console.WriteLine("g:{0}--m:{1}--x:{2}", x, y, z);
                        }
                    }
                }

    python代码-->

    class Calculate:
        def run():
            for x in range(1,20):
                for y in range(1,33):
                    z = 100-x-y
                    if 5*x +3*y +z/3 == 100:
                        print('g:{}--m:{}--x:{}'.format(x,y,z))
    
    Calculate.run()

    java-->

        private static void calculate() {
            for (int x = 1; x < 20; x++) {
                for (int y = 1; y < 33; y++) {
                    int z= 100-x-y;
                    if (5*x+3*y+z/3.0 == 100) {
                        System.out.println("g:"+x+"--m:"+y+
                                "--x:"+z);
                    }
                }
            }
        }

    以上复杂度为o(n2),进行优化-->

    y = 25 - 7 * x / 4

    0<3 * (25 - 7 * x / 4) < 100

    y为4的倍数

    z=100-x-y

    c#代码-->

                for (int x=4;x<14;x+=4)
                {
                    int y = 25 - 7 * x / 4;
                    int z = 100 - x - y;
                    Console.WriteLine("g:{0}--m:{1}--x:{2}", x, y, z);
                }

    python和java代码同理

    复杂度为O(n)

  • 相关阅读:
    USACO 3.1
    linux 逻辑卷管理 调整分区大小
    记录一下
    ADOX创建ACCESS数据库列名的数据类型
    使用jstack分析cpu消耗过高的问题
    fastadmin添加定时任务
    linux定时任务
    技术域
    IOS div上下滑动效果
    mysql根据时间统计数据语句
  • 原文地址:https://www.cnblogs.com/ouyangping/p/8537280.html
Copyright © 2020-2023  润新知