给定墙的长度w和两个长度m和n的架子,在最优解中找出每种类型架子的数量和剩余的空位,使空位最小。两个货架中较大的一个更便宜,所以它是首选。然而,成本是次要的,首要任务是尽量减少墙上的空间。
null
例如:
Input : w = 24 m = 3 n = 5Output : 3 3 0We use three units of both shelvesand 0 space is left.3 * 3 + 3 * 5 = 24So empty space = 24 - 24 = 0Another solution could have been 8 0 0but since the larger shelf of length 5is cheaper the former will be the answer.Input : w = 29 m = 3 n = 9 Output : 0 3 20 * 3 + 3 * 9 = 2729 - 27 = 2Input : w = 24 m = 4 n = 7 Output : 6 0 06 * 4 + 0 * 7 = 2424 - 24 = 0
一个简单而有效的方法是尝试所有适合墙壁长度的架子组合。 为了实现这种方法,以及大货架成本低于小货架成本的限制,从0开始,我们增加了大类型货架的数量,直到它们能够适应。对于每种情况,我们都会计算空空间,并最终存储使空空间最小化的值。如果两种情况下的空位相同,我们更喜欢有更多空位的情况,而不是更大的货架。
下面是它的实现。
C++
// C++ program to find minimum space and units // of two shelves to fill a wall. #include <bits/stdc++.h> using namespace std; void minSpacePreferLarge( int wall, int m, int n) { // for simplicity, Assuming m is always smaller than n // initializing output variables int num_m = 0, num_n = 0, min_empty = wall; // p and q are no of shelves of length m and n // rem is the empty space int p = wall/m, q = 0, rem=wall%m; num_m=p; num_n=q; min_empty=rem; while (wall >= n) { // place one more shelf of length n q += 1; wall = wall - n; // place as many shelves of length m // in the remaining part p = wall / m; rem = wall % m; // update output variablse if curr // min_empty <= overall empty if (rem <= min_empty) { num_m = p; num_n = q; min_empty = rem; } } cout << num_m << " " << num_n << " " << min_empty << endl; } // Driver code int main() { int wall = 29, m = 3, n = 9; minSpacePreferLarge(wall, m, n); wall = 76, m = 1, n = 10; minSpacePreferLarge(wall, m, n); return 0; } |
JAVA
// Java program to count all rotation // divisible by 4. public class GFG { static void minSpacePreferLarge( int wall, int m, int n) { // For simplicity, Assuming m is always smaller than n // initializing output variables int num_m = 0 , num_n = 0 , min_empty = wall; // p and q are no of shelves of length m and n // rem is the empty space int p = wall/m, q = 0 , rem=wall%m; num_m=p; num_n=q; min_empty=rem; while (wall >= n) { q += 1 ; wall = wall - n; // place as many shelves of length m // in the remaining part p = wall / m; rem = wall % m; // update output variablse if curr // min_empty <= overall empty if (rem <= min_empty) { num_m = p; num_n = q; min_empty = rem; } // place one more shelf of length n q += 1 ; wall = wall - n; } System.out.println(num_m + " " + num_n + " " + min_empty); } // Driver Code public static void main(String[] args) { int wall = 24 , m = 3 , n = 5 ; minSpacePreferLarge(wall, m, n); wall = 24 ; m = 4 ; n = 7 ; minSpacePreferLarge(wall, m, n); } } // This code is contributed by Saket Kumar |
Python3
def minSpacePreferLarge(w, m, n): # initialize result variables num_m = 0 num_n = 0 rem = w # p and q are no of shelves of length m & # n respectively. r is the remainder uncovered # wall length p = w / / m q = 0 rem = w % m; num_m = p; num_n = q; min_empty = rem; while (w > = n): q + = 1 ; w - = n; p = w / / m r = w % m if (r < = rem): num_m = p num_n = q rem = r q + = 1 w - = n print ( str ( int (num_m)) + " " + str (num_n) + " " + str (rem)) # Driver code w = 24 m = 3 n = 5 minSpacePreferLarge(w, m, n) w = 24 m = 4 n = 7 minSpacePreferLarge(w, m, n) |
C#
// C# program to count all rotation // divisible by 4. using System; class GFG { static void minSpacePreferLarge( int wall, int m, int n) { // For simplicity, Assuming m is always smaller than n // initializing output variables int num_m = 0, num_n = 0, min_empty = wall; // p and q are no of shelves of length m and n // rem is the empty space int p = wall/m, q = 0, rem=wall%m; num_m=p; num_n=q; min_empty=rem; while (wall >= n) { q += 1; wall = wall - n; // place as many shelves of length m // in the remaining part p = wall / m; rem = wall % m; // update output variablse if curr // min_empty <= overall empty if (rem <= min_empty) { num_m = p; num_n = q; min_empty = rem; } // place one more shelf of length n q += 1; wall = wall - n; } Console.WriteLine(num_m + " " + num_n + " " + min_empty); } // Driver code static public void Main() { int wall = 24, m = 3, n = 5; minSpacePreferLarge(wall, m, n); wall = 24; m = 4; n = 7; minSpacePreferLarge(wall, m, n); } } // This code is contributed by Tushil. |
PHP
<?php // PHP program to find minimum space and units // of two shelves to fill a wall. function minSpacePreferLarge( $wall , $m , $n ) { // for simplicity, Assuming m is always smaller than n // initializing output variables $num_m = 0; $num_n = 0; $min_empty = $wall ; // p and q are no of shelves of length m and n // rem is the empty space $p = $wall / $m $q = 0 $rem = $wall % $m ; $num_m = $p ; $num_n = $q ; $min_empty = $rem ; while ( $wall >= $n ) { $q += 1; $wall = $wall - $n ; // place as many shelves of length m // in the remaining part $p = $wall / $m ; $rem = $wall % $m ; // update output variablse if curr // min_empty <= overall empty if ( $rem <= $min_empty ) { $num_m = $p ; $num_n = $q ; $min_empty = $rem ; } // place one more shelf of length n $q += 1; $wall = $wall - $n ; } echo $num_m , " " , $num_n , " " , $min_empty , "" ; } // Driver code $wall = 24; $m = 3; $n = 5; minSpacePreferLarge( $wall , $m , $n ); $wall = 24; $m = 4; $n = 7; minSpacePreferLarge( $wall , $m , $n ); // This code is contributed by ajit. ?> |
Javascript
<script> // Javascript program to count all rotation divisible by 4. function minSpacePreferLarge(wall, m, n) { // for simplicity, Assuming m is always smaller than n // initializing output variables let num_m = 0, num_n = 0, min_empty = wall; // p and q are no of shelves of length m and n // rem is the empty space let p = parseInt(wall/m, 10), q = 0, rem=wall%m; num_m=p; num_n=q; min_empty=rem; while (wall >= n) { // place one more shelf of length n q += 1; wall = wall - n; // place as many shelves of length m // in the remaining part p = parseInt(wall / m, 10); rem = wall % m; // update output variablse if curr // min_empty <= overall empty if (rem <= min_empty) { num_m = p; num_n = q; min_empty = rem; } } document.write(num_m + " " + num_n + " " + min_empty + "</br>" ); } let wall = 29, m = 3, n = 9; minSpacePreferLarge(wall, m, n); wall = 76, m = 1, n = 10; minSpacePreferLarge(wall, m, n); </script> |
输出
0 3 26 7 0
工具书类 :Sumologic实习问题 本文由 阿迪蒂·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END