给定正整数n,请找到小于或等于n的3或7的所有倍数的计数。
例子 :
Input : n = 10
Output : Count = 4
The multiples are 3, 6, 7 and 9
Input : n = 25
Output : Count = 10
The multiples are 3, 6, 7, 9, 12, 14, 15, 18, 21 and 24
一个简单的解决方案是遍历从1到n的所有数字,并在数字为3或7或两者的倍数时递增计数。
C++
// A Simple C++ program to find count of all
// numbers that multiples
#include
using namespace std;
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
int res = 0;
for (int i=1; i<=n; i++)
if (i%3==0 || i%7 == 0)
res++;
return res;
}
// Driver code
int main()
{
cout << "Count = " << countMultiples(25);
}
Java
// A Simple Java program to
// find count of all numbers
// that multiples
import java.io.*;
class GFG
{
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
int res = 0;
for (int i = 1; i <= n; i++)
if (i % 3 == 0 || i % 7 == 0)
res++;
return res;
}
// Driver Code
public static void main (String[] args)
{
System.out.print("Count = ");
System.out.println(countMultiples(25));
}
}
// This code is contributed by m_kit
Python3
# A Simple Python3 program to
# find count of all numbers
# that multiples
# Returns count of all numbers
# smaller than or equal to n
# and multples of 3 or 7 or both
def countMultiples(n):
res = 0;
for i in range(1, n + 1):
if (i % 3 == 0 or i % 7 == 0):
res += 1;
return res;
# Driver code
print("Count =", countMultiples(25));
# This code is contributed by mits
C#
// A Simple C# program to
// find count of all numbers
// that are multiples of 3 or 7
using System;
class GFG
{
// Returns count of all
// numbers smaller than
// or equal to n and
// are multples of 3 or
// 7 or both
static int countMultiples(int n)
{
int res = 0;
for (int i = 1; i <= n; i++)
if (i % 3 == 0 || i % 7 == 0)
res++;
return res;
}
// Driver Code
static public void Main ()
{
Console.Write("Count = ");
Console.WriteLine(countMultiples(25));
}
}
// This code is contributed by ajit
PHP
Javascript
C++
// A better C++ program to find count of all
// numbers that multiples
#include
using namespace std;
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
return n/3 + n/7 -n/21;
}
// Driver code
int main()
{
cout << "Count = " << countMultiples(25);
}
Java
// A better Java program to
// find count of all numbers
// that multiples
import java.io.*;
class GFG
{
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
return n / 3 + n / 7 - n / 21;
}
// Driver code
public static void main (String args [] )
{
System.out.println("Count = " +
countMultiples(25));
}
}
// This code is contributed by aj_36
Python 3
# Python 3 program to find count of
# all numbers that multiples
# Returns count of all numbers
# smaller than or equal to n and
# multples of 3 or 7 or both
def countMultiples(n):
return n / 3 + n / 7 - n / 21;
# Driver code
n = ((int)(countMultiples(25)));
print("Count =", n);
# This code is contributed
# by Shivi_Aggarwal
C#
// A better Java program to
// find count of all numbers
// that multiples
using System;
class GFG
{
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
return n / 3 + n / 7 - n / 21;
}
// Driver Code
static public void Main ()
{
Console.WriteLine("Count = " +
countMultiples(25));
}
}
// This code is contributed by m_kit
PHP
Javascript
输出 :
Count = 10
时间复杂度: O(n)
一个有效的解决方案可以在O(1)时间内解决上述问题。这个想法是计算3的倍数,再加上7的倍数,然后减去21的倍数,因为它们被计数两次。
count = n/3 + n/7 - n/21
C++
// A better C++ program to find count of all
// numbers that multiples
#include
using namespace std;
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
return n/3 + n/7 -n/21;
}
// Driver code
int main()
{
cout << "Count = " << countMultiples(25);
}
Java
// A better Java program to
// find count of all numbers
// that multiples
import java.io.*;
class GFG
{
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
return n / 3 + n / 7 - n / 21;
}
// Driver code
public static void main (String args [] )
{
System.out.println("Count = " +
countMultiples(25));
}
}
// This code is contributed by aj_36
的Python 3
# Python 3 program to find count of
# all numbers that multiples
# Returns count of all numbers
# smaller than or equal to n and
# multples of 3 or 7 or both
def countMultiples(n):
return n / 3 + n / 7 - n / 21;
# Driver code
n = ((int)(countMultiples(25)));
print("Count =", n);
# This code is contributed
# by Shivi_Aggarwal
C#
// A better Java program to
// find count of all numbers
// that multiples
using System;
class GFG
{
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
return n / 3 + n / 7 - n / 21;
}
// Driver Code
static public void Main ()
{
Console.WriteLine("Count = " +
countMultiples(25));
}
}
// This code is contributed by m_kit
的PHP
Java脚本
输出 :
Count = 10