Dados los números enteros n , a , b y c , la tarea es encontrar el valor máximo de x + y + z tal que ax + by + cz = n .
Ejemplos:
Entrada:
n = 10
a = 5
b = 3
c = 4
Salida:
3
Explicación:
x = 0, y = 2 y z = 1Entrada:
n = 50
a = 8
b = 10
c = 2
Salida:
25
Explicación:
x = 0, y = 0 y z = 25
Enfoque: fije los valores de x e y , luego el valor de z se puede calcular como z = (n – (ax + by)) / c . Si el valor actual de z es un número entero, actualice el valor máximo de x + y + z encontrado hasta ahora.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) int maxResult(int n, int a, int b, int c) { int maxVal = 0; // i represents possible values of a * x for (int i = 0; i <= n; i += a) { // j represents possible values of b * y for (int j = 0; j <= n - i; j += b) { float z = (float)(n - (i + j)) / (float)(c); // If z is an integer if (floor(z) == ceil(z)) { int x = i / a; int y = j / b; maxVal = max(maxVal, x + y + (int)z); } } } return maxVal; } // Driver code int main() { int n = 10, a = 5, b = 3, c = 4; // Function Call cout << maxResult(n, a, b, c); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) static int maxResult(int n, int a, int b, int c) { int maxVal = 0; // i represents possible values of a * x for (int i = 0; i <= n; i += a) // j represents possible values of b * y for (int j = 0; j <= n - i; j += b) { float z = (float)(n - (i + j)) / (float)c; // If z is an integer if (Math.floor(z) == Math.ceil(z)) { int x = i / a; int y = j / b; maxVal = Math.max(maxVal, x + y + (int)z); } } return maxVal; } // Driver code public static void main(String args[]) { int n = 10, a = 5, b = 3, c = 4; // Function Call System.out.println(maxResult(n, a, b, c)); } } // This code is contributed by // Surendra_Gangwar
Python3
# Python3 implementation of the approach from math import * # Function to return the maximum value # of (x + y + z) such that (ax + by + cz = n) def maxResult(n, a, b, c): maxVal = 0 # i represents possible values of a * x for i in range(0, n + 1, a): # j represents possible values of b * y for j in range(0, n - i + 1, b): z = (n - (i + j)) / c # If z is an integer if (floor(z) == ceil(z)): x = i // a y = j // b maxVal = max(maxVal, x + y + int(z)) return maxVal # Driver code if __name__ == "__main__": n = 10 a = 5 b = 3 c = 4 # Function Call print(maxResult(n, a, b, c)) # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) static int maxResult(int n, int a, int b, int c) { int maxVal = 0; // i represents possible values of a * x for (int i = 0; i <= n; i += a) // j represents possible values of b * y for (int j = 0; j <= n - i; j += b) { float z = (float)(n - (i + j)) / (float)c; // If z is an integer if (Math.Floor(z) == Math.Ceiling(z)) { int x = i / a; int y = j / b; maxVal = Math.Max(maxVal, x + y + (int)z); } } return maxVal; } // Driver code public static void Main(String[] args) { int n = 10, a = 5, b = 3, c = 4; // Function Call Console.WriteLine(maxResult(n, a, b, c)); } } // This code has been contributed by 29AjayKumar
PHP
<?php // PHP implementation of the approach // Function to return the maximum value of // (x + y + z) such that (ax + by + cz = n) function maxResult($n, $a, $b, $c) { $maxVal = 0; // i represents possible values of a * x for ($i = 0; $i <= $n; $i += $a) // j represents possible values of b * y for ($j = 0; $j <= $n - $i; $j += $b) { $z = ($n - ($i + $j)) / $c; // If z is an integer if (floor($z) == ceil($z)) { $x = (int)($i / $a); $y = (int)($j / $b); $maxVal = max($maxVal, $x + $y + (int)$z); } } return $maxVal; } // Driver code $n = 10; $a = 5; $b = 3; $c = 4; echo maxResult($n, $a, $b, $c); // This code is contributed by mits ?>
Javascript
<script> // Javascript implementation of the above approach // Function to return the maximum value of (x + y + z) // such that (ax + by + cz = n) function maxResult(n, a, b, c) { let maxVal = 0; // i represents possible values of a * x for (let i = 0; i <= n; i += a) // j represents possible values of b * y for (let j = 0; j <= n - i; j += b) { let z = (n - (i + j)) / c; // If z is an integer if (Math.floor(z) == Math.ceil(z)) { let x = i / a; let y = j / b; maxVal = Math.max(maxVal, x + y + z); } } return maxVal; } // driver program let n = 10, a = 5, b = 3, c = 4; // Function Call document.write(maxResult(n, a, b, c)); </script>
3
Complejidad del tiempo: O(N 2 )
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA