给定三个正整数x,y和n,任务是查找可以使用x和y形成的从1到n的所有数字的计数。如果我们可以通过添加任意数量的x和/或y来获得数字,则可以使用x和y形成数字。
例子 :
Input : n = 10, x = 2, y = 3
Output : 9
We can form 9 out of 10 numbers using 2 and 3
2 = 2, 3 = 3, 4 = 2+2, 5 = 2+3, 6 = 3+3
7 = 2+2+3, 8 = 3+3+2, 9 = 3+3+3
and 10 = 3+3+2+2.
Input : n = 10, x = 5, y = 7
Output : 3
We can form 3 out of 10 numbers using 5 and 7
The numbers are 5, 7 and 10
Input : n = 15, x = 5, y = 7
Output : 6
We can form 6 out of 10 numbers using 5 and 7.
The numbers are 5, 7, 10, 12, 14 and 15.
Input : n = 15, x = 2, y = 4
Output : 7
一个简单的解决方案是编写一个以0开头并进行两次递归调用的递归代码。一个递归调用添加x,其他添加y。这样,我们计算总数。我们需要确保一个数字被多次计数。
一个有效的解决方案是使用大小为n + 1的布尔数组arr []。项arr [i] = true表示可以使用x和y构成我。如果x和y小于或等于n,则将arr [x]和arr [y]初始化为true。我们从两个数字中较小的那个开始遍历数组,并用x和y逐一标记所有数字。下面是实现。
C++
// C++ program to count all numbers that can
// be formed using two number numbers x an y
#include
using namespace std;
// Returns count of numbers from 1 to n that can be formed
// using x and y.
int countNums(int n, int x, int y)
{
// Create an auxiliary array and initialize it
// as false. An entry arr[i] = true is going to
// mean that i can be formed using x and y
vector arr(n+1, false);
// x and y can be formed using x and y.
if (x <= n)
arr[x] = true;
if (y <= n)
arr[y] = true;
// Initialize result
int result = 0;
// Traverse all numbers and increment
// result if a number can be formed using
// x and y.
for (int i=min(x, y); i<=n; i++)
{
// If i can be formed using x and y
if (arr[i])
{
// Then i+x and i+y can also be formed
// using x and y.
if (i+x <= n)
arr[i+x] = true;
if (i+y <= n)
arr[i+y] = true;
// Increment result
result++;
}
}
return result;
}
// Driver code
int main()
{
int n = 15, x = 5, y = 7;
cout << countNums(n, x, y);
return 0;
}
Java
// Java program to count all numbers that can
// be formed using two number numbers x an y
class gfg{
// Returns count of numbers from 1 to n that can be formed
// using x and y.
static int countNums(int n, int x, int y)
{
// Create an auxiliary array and initialize it
// as false. An entry arr[i] = true is going to
// mean that i can be formed using x and y
boolean[] arr=new boolean[n+1];
// x and y can be formed using x and y.
if (x <= n)
arr[x] = true;
if (y <= n)
arr[y] = true;
// Initialize result
int result = 0;
// Traverse all numbers and increment
// result if a number can be formed using
// x and y.
for (int i=Math.min(x, y); i<=n; i++)
{
// If i can be formed using x and y
if (arr[i])
{
// Then i+x and i+y can also be formed
// using x and y.
if (i+x <= n)
arr[i+x] = true;
if (i+y <= n)
arr[i+y] = true;
// Increment result
result++;
}
}
return result;
}
// Driver code
public static void main(String[] args)
{
int n = 15, x = 5, y = 7;
System.out.println(countNums(n, x, y));
}
}
// This code is contributed by mits
Python3
# Python3 program to count all numbers
# that can be formed using two number
# numbers x an y
# Returns count of numbers from 1
# to n that can be formed using x and y.
def countNums(n, x, y):
# Create an auxiliary array and
# initialize it as false. An
# entry arr[i] = True is going to
# mean that i can be formed using
# x and y
arr = [False for i in range(n + 2)]
# x and y can be formed using x and y.
if(x <= n):
arr[x] = True
if(y <= n):
arr[y] = True
# Initialize result
result = 0
# Traverse all numbers and increment
# result if a number can be formed
# using x and y.
for i in range(min(x, y), n + 1):
# If i can be formed using x and y
if(arr[i]):
# Then i+x and i+y can also
# be formed using x and y.
if(i + x <= n):
arr[i + x] = True
if(i + y <= n):
arr[i + y] = True
# Increment result
result = result + 1
return result
# Driver code
n = 15
x = 5
y = 7
print(countNums(n, x, y))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to count all numbers that can
// be formed using two number numbers x an y
using System;
public class GFG{
// Returns count of numbers from 1 to n that can be formed
// using x and y.
static int countNums(int n, int x, int y)
{
// Create an auxiliary array and initialize it
// as false. An entry arr[i] = true is going to
// mean that i can be formed using x and y
bool []arr=new bool[n+1];
// x and y can be formed using x and y.
if (x <= n)
arr[x] = true;
if (y <= n)
arr[y] = true;
// Initialize result
int result = 0;
// Traverse all numbers and increment
// result if a number can be formed using
// x and y.
for (int i=Math.Min(x, y); i<=n; i++)
{
// If i can be formed using x and y
if (arr[i])
{
// Then i+x and i+y can also be formed
// using x and y.
if (i+x <= n)
arr[i+x] = true;
if (i+y <= n)
arr[i+y] = true;
// Increment result
result++;
}
}
return result;
}
// Driver code
static public void Main (){
int n = 15, x = 5, y = 7;
Console.WriteLine(countNums(n, x, y));
}
}
PHP
Javascript
输出 :
6