给定三个数字b,x,n。任务是找到等式(a + b)<= n中的’a’值,以使a + b被x整除。如果没有这样的值,则打印-1。
例子:
Input: b = 10, x = 6, n = 40
Output: 2 8 14 20 26
Input: b = 10, x = 1, n = 10
Output: -1
方法:人们可以找到最小的可能值(b / x + 1)* x – b。然后我们将答案增加x,直到不大于n。这里(b / x +1)* x是被x整除的最小可能值。
下面是上述方法的实现:
C++
// CPP program to Find values of a, in equation
// (a+b)<=n and a+b is divisible by x.
#include
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;
}
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
= 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
?>
输出:
2 8 14 20 26
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。