找到最接近n且可被m整除的数字

给定两个整数 N M .问题是找到最接近的数字 N 并且可以被 M .如果有多个这样的数字,则输出具有最大绝对值的数字。如果 N 完全可以被 M ,然后输出 N 只有要求时间复杂度为O(1)。 限制条件: m!=0 例如:

null
Input : n = 13, m = 4Output : 12Input : n = -15, m = 6Output : -18Both -12 and -18 are closest to -15, but-18 has the maximum absolute value.

资料来源: 微软面试经验|设置125 .

我们找到n/m的值。让这个值为q。然后我们找到两种可能性中最接近的。一个是q*m,另一个是(m*(q+1))或(m*(q-1)),这取决于给定的两个数字中是否有一个是负数。 算法:

closestNumber(n, m)    Declare q, n1, n2    q = n / m    n1 = m * q    if (n * m) > 0        n2 = m * (q + 1)    else        n2 = m * (q - 1)    if abs(n-n1) < abs(n-n2)        return n1    return n2  

C++

// C++ implementation to find the number closest to n
// and divisible by m
#include <bits/stdc++.h>
using namespace std;
// function to find the number closest to n
// and divisible by m
int closestNumber( int n, int m)
{
// find the quotient
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the required closest number
if ( abs (n - n1) < abs (n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
// Driver program to test above
int main()
{
int n = 13, m = 4;
cout << closestNumber(n, m) << endl;
n = -15; m = 6;
cout << closestNumber(n, m) << endl;
n = 0; m = 8;
cout << closestNumber(n, m) << endl;
n = 18; m = -7;
cout << closestNumber(n, m) << endl;
return 0;
}


JAVA

// Java implementation to find the number closest to n
// and divisible by m
public class close_to_n_divisible_m {
// function to find the number closest to n
// and divisible by m
static int closestNumber( int n, int m)
{
// find the quotient
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1 )) : (m * (q - 1 ));
// if true, then n1 is the required closest number
if (Math.abs(n - n1) < Math.abs(n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
// Driver program to test above
public static void main(String args[])
{
int n = 13 , m = 4 ;
System.out.println(closestNumber(n, m));
n = - 15 ; m = 6 ;
System.out.println(closestNumber(n, m));
n = 0 ; m = 8 ;
System.out.println(closestNumber(n, m));
n = 18 ; m = - 7 ;
System.out.println(closestNumber(n, m));
}
}
// This code is contributed by Sumit Ghosh


Python3

# Python 3 implementation to find
# the number closest to n
# Function to find the number closest
# to n and divisible by m
def closestNumber(n, m) :
# Find the quotient
q = int (n / m)
# 1st possible closest number
n1 = m * q
# 2nd possible closest number
if ((n * m) > 0 ) :
n2 = (m * (q + 1 ))
else :
n2 = (m * (q - 1 ))
# if true, then n1 is the required closest number
if ( abs (n - n1) < abs (n - n2)) :
return n1
# else n2 is the required closest number
return n2
# Driver program to test above
n = 13 ; m = 4
print (closestNumber(n, m))
n = - 15 ; m = 6
print (closestNumber(n, m))
n = 0 ; m = 8
print (closestNumber(n, m))
n = 18 ; m = - 7
print (closestNumber(n, m))
# This code is contributed by Nikita tiwari.


C#

// C# implementation to find the
// number closest to n and divisible by m
using System;
class GFG {
// function to find the number closest to n
// and divisible by m
static int closestNumber( int n, int m)
{
// find the quotient
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the required closest number
if (Math.Abs(n - n1) < Math.Abs(n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
// Driver program to test above
public static void Main()
{
int n = 13, m = 4;
Console.WriteLine(closestNumber(n, m));
n = -15;
m = 6;
Console.WriteLine(closestNumber(n, m));
n = 0;
m = 8;
Console.WriteLine(closestNumber(n, m));
n = 18;
m = -7;
Console.WriteLine(closestNumber(n, m));
}
}
// This code is contributed by Sam007


PHP

<?php
// PHP implementation to find
// the number closest to n and
// divisible by m
// function to find the number
// closest to n and divisible by m
function closestNumber( $n , $m )
{
// find the quotient
$q = (int) ( $n / $m );
// 1st possible closest number
$n1 = $m * $q ;
// 2nd possible closest number
$n2 = ( $n * $m ) > 0 ?
( $m * ( $q + 1)) : ( $m * ( $q - 1));
// if true, then n1 is the
// required closest number
if ( abs ( $n - $n1 ) < abs ( $n - $n2 ))
return $n1 ;
// else n2 is the required
// closest number
return $n2 ;
}
// Driver Code
$n = 13;
$m = 4;
echo closestNumber( $n , $m ), "" ;
$n = -15;
$m = 6;
echo closestNumber( $n , $m ), "" ;
$n = 0;
$m = 8;
echo closestNumber( $n , $m ), "" ;
$n = 18;
$m = -7;
echo closestNumber( $n , $m ), "" ;
// This code is contributed by jit_t
?>


Javascript

<script>
// Javascript implementation to find
// the number closest to n and
// divisible by m
// function to find the number
// closest to n and divisible by m
function closestNumber(n, m)
{
// find the quotient
let q = parseInt(n / m);
// 1st possible closest number
let n1 = m * q;
// 2nd possible closest number
let n2 = (n * m) > 0 ?
(m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the
// required closest number
if (Math.abs(n - n1) < Math.abs(n - n2))
return n1;
// else n2 is the required
// closest number
return n2;
}
// Driver Code
let n = 13;
let m = 4;
document.write(closestNumber(n, m) + "<br>" );
n = -15;
m = 6;
document.write(closestNumber(n, m) + "<br>" );
n = 0;
m = 8;
document.write(closestNumber(n, m) + "<br>" );
n = 18;
m = -7;
document.write(closestNumber(n, m) + "<br>" );
// This code is contributed by gfgking
</script>


输出:

12-18021

时间复杂性: O(1) 本文由 阿尤什·焦哈里 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享