原文:
给定三个数字 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
python 3
# 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
服务器端编程语言(professional hypertext preprocessor 的缩写)
= 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
?>
java 描述语言
output:
2 8 14 20 26
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处