给定两个整数A和N ,我们的任务是找到不能被A整除的第N个自然数。
例子:
Input: A = 4, N = 12
Output: 15
Explanation:
The series starting from 1 excluding the multiples of A would be 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17 and the 12th term which is not divisible by 4 is 15.
Input: A = 3, N = 20
Output: 29
Explanation:
The series starting from 1 excluding the multiples of A would be 1, 2, 4, 5, 7, 8, 10, 11 and so on and the Nth number which is not divisible by 3 is 29.
方法:
为了解决上述问题,我们必须观察到,由于跳过了A的倍数,所以每个(A – 1)整数之后都有一个间隙。为了找到数字N,我们将N除以(A – 1),然后将其存储在变量中,我们说商。现在将A与该变量相乘,然后将余数相加,我们将得到结果答案。
If A = 4, N = 7:
quotient = 7 / 3 = 2
remainder = 7 % 3 = 1
So the answer is
(A * quotient) + remainder
= 4 * 2 + 1 = 9
但是,如果余数为0,则表示它是集合中的最后一个元素。让我们通过一个例子来理解它。
If A = 4, N = 6:
quotient = 6 / 3 = 2
remainder = 6 % 3 = 0
So the answer is
(A * quotient) - 1
= 4 * 2 - 1 = 7
下面是上述方法的实现:
C++
// C++ code to Find the Nth number which
// is not divisible by A from the series
#include
using namespace std;
void findNum(int n, int k)
{
// Find the quotient and the remainder
// when k is divided by n-1
int q = k / (n - 1);
int r = k % (n - 1);
int a;
// If the remainder is not 0
// multiply n by q and subtract 1
// if remainder is 0 then
// multiply n by q
// and add the remainder
if(r != 0)
a = (n * q) + r;
else
a = (n * q) - 1;
// Print the answer
cout << a;
}
// Driver code
int main()
{
int A = 4, N = 6;
findNum(A, N);
return 0;
}
// This code is contributed by PratikBasu
Java
// Java code to Find the Nth number which
// is not divisible by A from the series
class GFG {
static void findNum(int n, int k)
{
// Find the quotient and the remainder
// when k is divided by n-1
int q = k / (n - 1);
int r = k % (n - 1);
int a = 0;
// If the remainder is not 0
// multiply n by q and subtract 1
// if remainder is 0 then
// multiply n by q
// and add the remainder
if (r != 0)
a = (n * q) + r;
else
a = (n * q) - 1;
// Print the answer
System.out.println(a);
}
// Driver code
public static void main(String[] args)
{
int A = 4;
int N = 6;
findNum(A, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 code to Find the Nth number which
# is not divisible by A from the series
def findNum(n, k):
# Find the quotient and the remainder
# when k is divided by n-1
q = k//(n-1)
r = k % (n-1)
# if the remainder is not 0
# multiply n by q and subtract 1
# if remainder is 0 then
# multiply n by q
# and add the remainder
if(r != 0):
a = (n * q)+r
else:
a = (n * q)-1
# print the answer
print(a)
# driver code
A = 4
N = 6
findNum(A, N)
C#
// C# code to find the Nth number which
// is not divisible by A from the series
using System;
class GFG{
static void findNum(int n, int k)
{
// Find the quotient and the remainder
// when k is divided by n-1
int q = k / (n - 1);
int r = k % (n - 1);
int a = 0;
// If the remainder is not 0
// multiply n by q and subtract 1
// if remainder is 0 then
// multiply n by q
// and add the remainder
if (r != 0)
a = (n * q) + r;
else
a = (n * q) - 1;
// Print the answer
Console.WriteLine(a);
}
// Driver code
public static void Main(String[] args)
{
int A = 4;
int N = 6;
findNum(A, N);
}
}
// This code is contributed by amal kumar choubey
输出:
7