折腾两个壶的面试题怎么做?

问:

两个任意容量水壶,能否恰好量出目标分量的水?

相当经典面试题,小学的时候就开始在寒假作业里折腾我了,想不到现在还成了面试的拦路虎。所以决定彻底的解决掉。

如果实际问题的核心就是最大公约数

见代码中:GCD 函数

然后就很简单了。

 1 using System;
 2 
 3 namespace Algorithm.BottleAndBottle
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             var x = Ask("Please Input First The Bottle Capacity");
10             var y = Ask("Please Input Second The Bottle Capacity");
11             var max = x + y;
12 
13             var z = Ask("Please Input Target Request Capacity");
14             if (z > max) { Console.WriteLine(" Target Is Overload!"); return; }
15 
16             //GET GCD
17             var gcd = GCD(x, y);
18 
19             if (z % gcd > 0)
20             {
21                 Console.WriteLine("Can't Not Load");
22                 Console.ReadLine();
23                 return;
24             }
25 
26             int a, b;
27             //z=a*x+b*y;
28 
29             int[,] aLine = ValueLine(x, y), bLine = ValueLine(y, x);
30             a = 2 * y - 1;
31             b = 2 * x - 1;
32 
33             for (int xi = 0; xi < a; xi++)
34             {
35                 var xIndex = aLine[0, xi];
36                 var xValue = aLine[1, xi];
37 
38                 for (int yi = 0; yi < b; yi++)
39                 {
40                     var yIndex = bLine[0, yi];
41                     var yValue = bLine[1, yi];
42 
43                     var sum = xValue + yValue;
44 
45                     if (sum > 0 && sum <= max)
46                     {
47                         var s = $"a:{xIndex};b:{yIndex}=>{sum}";
48                         if (sum == z)
49                         {
50                             Console.WriteLine(s + " <<== LIKE THIS");
51                         }
52                         else
53                         {
54                             Console.WriteLine(s);
55                         }
56                     }
57                 }
58             }
59         }
60 
61         static int[,] ValueLine(int v, int c)
62         {
63             int[,] Line = new int[2, 2 * c - 1];
64             int index = 1 - c;
65             int vs = v * index;
66             int count = 2 * c - 1;
67 
68             for (int i = 0; i < count; i++)
69             {
70                 Line[0, i] = index++;
71                 Line[1, i] = vs;
72                 vs += v;
73             }
74             return Line;
75         }
76 
77         static int GCD(int a, int b)
78         {
79             return (a == b) ? a : ((a > b) ? GCD(a - b, b) : GCD(b - a, a));
80         }
81 
82 
83         static int Ask(string question)
84         {
85             int value;
86             string input;
87             bool isOk;
88             do
89             {
90                 Console.WriteLine(question);
91                 input = Console.ReadLine();
92                 isOk = (int.TryParse(input, out value) && value > 0);
93             } while (!isOk);
94 
95             return value;
96         }
97     }
98 
99 }

 

posted on 2020-05-09 15:57  上班很辛苦  阅读(56)  评论(0)    收藏  举报

导航