Imprimir valores de ‘a’ en la ecuación (a+b) <= n y a+b es divisible por x

Dados tres números b, x, n. La tarea es encontrar los valores de ‘a’ en la ecuación (a+b) <= n tal que a+b sea divisible por x. Si tales valores no son posibles, imprima -1.
Ejemplos: 
 

Input: b = 10, x = 6, n = 40
Output: 2 8 14 20 26

Input: b = 10, x = 1, n = 10
Output: -1

Enfoque: Uno puede encontrar el menor valor posible (b/x + 1)*x – b. Luego aumentamos la respuesta en x hasta que no sea mayor que n. Aquí (b/x + 1)*x es el valor mínimo posible que es divisible por x.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// CPP program to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
#include <bits/stdc++.h>
using namespace std;
 
// function to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
void PossibleValues(int b, int x, int n)
{
    // least possible which is divisible by x
    int leastdivisible = (b / x + 1) * x;
 
    int flag = 1;
 
    // run a loop to get required answer
    while (leastdivisible <= n) {
 
        if (leastdivisible - b >= 1) {
            cout << leastdivisible - b << " ";
 
            // increase value by x
            leastdivisible += x;
 
            // answer is possible
            flag = 0;
        }
        else
            break;
    }
 
    if (flag)
        cout << -1;
}
 
// Driver code
int main()
{
    int b = 10, x = 6, n = 40;
 
    // function call
    PossibleValues(b, x, n);
 
    return 0;
}

C

// C program to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
#include <stdio.h>
 
// function to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
void PossibleValues(int b, int x, int n)
{
    // least possible which is divisible by x
    int leastdivisible = (b / x + 1) * x;
 
    int flag = 1;
 
    // run a loop to get required answer
    while (leastdivisible <= n) {
 
        if (leastdivisible - b >= 1) {
            printf("%d ",leastdivisible - b);
 
            // increase value by x
            leastdivisible += x;
 
            // answer is possible
            flag = 0;
        }
        else
            break;
    }
 
    if (flag)
        printf("%d",-1);
}
 
// Driver code
int main()
{
    int b = 10, x = 6, n = 40;
 
    // function call
    PossibleValues(b, x, n);
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java program to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
 
import java.io.*;
 
class GFG {
  
// function to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
static void PossibleValues(int b, int x, int n)
{
    // least possible which is divisible by x
    int leastdivisible = (b / x + 1) * x;
 
    int flag = 1;
 
    // run a loop to get required answer
    while (leastdivisible <= n) {
 
        if (leastdivisible - b >= 1) {
            System.out.print( leastdivisible - b + " ");
 
            // increase value by x
            leastdivisible += x;
 
            // answer is possible
            flag = 0;
        }
        else
            break;
    }
 
    if (flag>0)
         System.out.println(-1);
}
 
// Driver code
    public static void main (String[] args) {
            int b = 10, x = 6, n = 40;
 
    // function call
    PossibleValues(b, x, n);
    }
}
 
 
// This code is contributed
// by shs

Python3

# Python3 program to Find values of a, in equation
# (a+b)<=n and a+b is divisible by x.
 
# function to Find values of a, in equation
# (a+b)<=n and a+b is divisible by x.
def PossibleValues(b, x, n) :
 
    # least possible which is divisible by x
    leastdivisible = int(b / x + 1) * x
 
    flag = 1
 
    # run a loop to get required answer
    while (leastdivisible <= n) :
 
        if (leastdivisible - b >= 1) :
            print(leastdivisible - b ,end= " ")
 
            # increase value by x
            leastdivisible += x
 
            # answer is possible
            flag = 0
         
        else :
            break
     
 
    if (flag != 0) :
        print(-1)
 
# Driver code
if __name__=='__main__':
    b = 10
    x = 6
    n = 40
# function call
    PossibleValues(b, x, n)
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to Find values of a,
// in equation (a+b)<=n and a+b
// is divisible by x.
using System;
 
class GFG {
     
// function to Find values
// of a, in equation (a+b)<=n
// and a+b is divisible by x.
static void PossibleValues(int b, int x, int n)
{
     
    // least possible which
    // is divisible by x
    int leastdivisible = (b / x + 1) * x;
 
    int flag = 1;
 
    // run a loop to get required answer
    while (leastdivisible <= n) {
 
        if (leastdivisible - b >= 1) {
             
            Console.Write( leastdivisible - b + " ");
 
            // increase value by x
            leastdivisible += x;
 
            // answer is possible
            flag = 0;
        }
        else
            break;
    }
 
    if (flag > 0)
        Console.WriteLine(-1);
}
 
    // Driver code
    public static void Main ()
    {
        int b = 10, x = 6, n = 40;
 
        // function call
        PossibleValues(b, x, n);
    }
}
 
// This code is contributed by Shubadeep

PHP

<?php
// PHP program to Find values of a,
// in equation (a+b)<=n and a+b is
// divisible by x.
 
// function to Find values of a,
// in equation (a+b)<=n and a+b
// is divisible by x.
function PossibleValues($b, $x, $n)
{
    // least possible which is
    // divisible by x
    $leastdivisible = (intval($b / $x) + 1) * $x;
 
    $flag = 1;
 
    // run a loop to get required answer
    while ($leastdivisible <= $n)
    {
 
        if ($leastdivisible - $b >= 1)
        {
            echo $leastdivisible - $b . " ";
 
            // increase value by x
            $leastdivisible += $x;
 
            // answer is possible
            $flag = 0;
        }
        else
            break;
    }
 
    if ($flag)
        echo "-1";
}
 
// Driver code
$b = 10;
$x = 6;
$n = 40;
 
// function call
PossibleValues($b, $x, $n);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
// Javascript program to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
     
    // function to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
    function PossibleValues(b,x,n)
    {
        // least possible which is divisible by x
    let leastdivisible = (Math.floor(b / x) + 1) * x;
   
    let flag = 1;
   
    // run a loop to get required answer
    while (leastdivisible <= n) {
   
        if (leastdivisible - b >= 1) {
            document.write( leastdivisible - b + " ");
   
            // increase value by x
            leastdivisible += x;
   
            // answer is possible
            flag = 0;
        }
        else
            break;
    }
   
    if (flag>0)
         document.write(-1+"<br>");
    }
     
    // Driver code
    let b = 10, x = 6, n = 40;
     
    // function call
    PossibleValues(b, x, n);
     
 
// This code is contributed by rag2127
</script>
Producción: 

2 8 14 20 26

 

Publicación traducida automáticamente

Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *