📌  相关文章
📜  检查数字 N 是否可以表示为 3、5 和 7 的倍数之和

📅  最后修改于: 2022-05-13 01:56:06.390000             🧑  作者: Mango

检查数字 N 是否可以表示为 3、5 和 7 的倍数之和

给定一个非负整数N ,任务是检查该整数是否可以表示为 3、5 和 7 的倍数的总和,并打印它们各自的值。如果任务不可行,则打印 -1。

例子:

朴素方法:给定问题可以通过使用三个嵌套的 for 循环来解决,迭代 3、5 和 7 的倍数,并跟踪是否存在 sum 为 N 的组合。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to check if a number can
// be represented as summation of the
// multiples of 3, 5 and 7
void check357(int N)
{
    // flag indicates if combination
    // is possible or not
    int flag = 0;
  
    // Loop for multiples of 3
    for (int i = 0; i <= N / 3; i++) {
  
        if (flag == 1)
            break;
  
        // Loop for multiples of 5
        for (int j = 0; j <= N / 5; j++) {
  
            if (flag == 1)
                break;
  
            // Loop for multiples of 7
            for (int k = 0; k <= N / 7; k++) {
  
                // If sum is N
                if (3 * i + 5 * j + 7 * k == N) {
  
                    // Combination found
                    flag = 1;
  
                    // Print Answer
                    cout << i << " "
                         << j << " " << k;
                    break;
                }
            }
        }
    }
  
    // No valid combination found
    if (flag == 0)
        cout << -1;
}
  
// Driver code
int main()
{
    int N = 10;
    check357(N);
}


Java
// Java code for the above approach
import java.io.*;
  
class GFG
{
  
  // Function to check if a number can
  // be represented as summation of the
  // multiples of 3, 5 and 7
  static void check357(int N)
  {
  
    // flag indicates if combination
    // is possible or not
    int flag = 0;
  
    // Loop for multiples of 3
    for (int i = 0; i <= N / 3; i++) {
  
      if (flag == 1)
        break;
  
      // Loop for multiples of 5
      for (int j = 0; j <= N / 5; j++) {
  
        if (flag == 1)
          break;
  
        // Loop for multiples of 7
        for (int k = 0; k <= N / 7; k++) {
  
          // If sum is N
          if (3 * i + 5 * j + 7 * k == N) {
  
            // Combination found
            flag = 1;
  
            // Print Answer
            System.out.print(i + " " + j + " "
                             + k);
            break;
          }
        }
      }
    }
  
    // No valid combination found
    if (flag == 0)
      System.out.println(-1);
  }
  
  // Driver code
  public static void main(String[] args)
  {
    int N = 10;
    check357(N);
  }
}
  
// This code is contributed by Potta Lokesh


Python3
# Python implementation of the above approach
  
# Function to check if a number can
# be represented as summation of the
# multiples of 3, 5 and 7
def check357(N):
  
  # flag indicates if combination
  # is possible or not
  flag = 0;
  
  # Loop for multiples of 3
  for i in range((N // 3) + 1): 
  
    if (flag == 1):
      break;
  
    # Loop for multiples of 5
    for j in range((N // 5) + 1):
  
      if (flag == 1):
        break;
  
      # Loop for multiples of 7
      for k in range((N // 7) + 1):
  
        # If sum is N
        if (3 * i + 5 * j + 7 * k == N):
  
          # Combination found
          flag = 1;
  
          # Print Answer
          print(f"{i} {j} {k}");
          break;
  
  # No valid combination found
  if (flag == 0):
    print(-1);
  
# Driver code
N = 10;
check357(N);
  
# This code is contributed by saurabh_jaiswal.


C#
// C# code for the above approach
using System;
  
public class GFG
{
  
  // Function to check if a number can
  // be represented as summation of the
  // multiples of 3, 5 and 7
  static void check357(int N)
  {
  
    // flag indicates if combination
    // is possible or not
    int flag = 0;
  
    // Loop for multiples of 3
    for (int i = 0; i <= N / 3; i++) {
  
      if (flag == 1)
        break;
  
      // Loop for multiples of 5
      for (int j = 0; j <= N / 5; j++) {
  
        if (flag == 1)
          break;
  
        // Loop for multiples of 7
        for (int k = 0; k <= N / 7; k++) {
  
          // If sum is N
          if (3 * i + 5 * j + 7 * k == N) {
  
            // Combination found
            flag = 1;
  
            // Print Answer
            Console.Write(i + " " + j + " " + k);
            break;
          }
        }
      }
    }
  
    // No valid combination found
    if (flag == 0)
      Console.WriteLine(-1);
  }
  
  // Driver code
  public static void Main(string[] args)
  {
    int N = 10;
    check357(N);
  }
}
  
// This code is contributed by AnkThon


Javascript


C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
int check357(int N)
{
    // Stores if a valid
    // combination exists
    int f = 0;
  
    // if N is of the form 3n
    if (N % 3 == 0)
        return (N / 3) * 100 + 0 * 10 + 0;
  
    else if (N % 3 == 1) {
  
        // if N is of the form 3n + 1
        if (N - 7 >= 0)
            return ((N - 7) / 3) * 100 + 0 * 10 + 1;
    }
    else
  
        // if N is of the form 3n + 2
        if (N - 5 >= 0)
        return ((N - 5) / 3) * 100 + 1 * 10 + 0;
  
    // If no valid combinations exists
    return -1;
}
  
// Driver code
int main()
{
    int N = 10;
    cout << check357(N);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
public class GFG
{
    
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
static int check357(int N)
{
    
    // Stores if a valid
    // combination exists
    int f = 0;
  
    // if N is of the form 3n
    if (N % 3 == 0)
        return (N / 3) * 100 + 0 * 10 + 0;
  
    else if (N % 3 == 1) {
  
        // if N is of the form 3n + 1
        if (N - 7 >= 0)
            return ((N - 7) / 3) * 100 + 0 * 10 + 1;
    }
    else
  
        // if N is of the form 3n + 2
        if (N - 5 >= 0)
        return ((N - 5) / 3) * 100 + 1 * 10 + 0;
  
    // If no valid combinations exists
    return -1;
}
  
// Driver code
public static void main(String args[])
{
    int N = 10;
    System.out.print(check357(N));
}
}
  
// This code is contributed by Samim Hossain Mondal.


Python3
# Python implementation of the above approach
  
# Function to check if a number can be
# represented as summation of multiples
# of 3, 5 and 7
def check357(N):
    
    # Stores if a valid
    # combination exists
    f = 0;
  
    # if N is of the form 3n
    if (N % 3 == 0):
        return (N // 3) * 100 + 0 * 10 + 0;
  
    elif (N % 3 == 1):
  
        # if N is of the form 3n + 1
        if (N - 7 >= 0):
            return ((N - 7) // 3) * 100 + 0 * 10 + 1;
    else:
        if(N - 5 >= 0):
            # if N is of the form 3n + 2
            return ((N - 5) // 3) * 100 + 1 * 10 + 0;
    # If no valid combinations exists
    return -1;
  
# Driver code
if __name__ == '__main__':
    N = 10;
    print(check357(N));
  
# This code is contributed by shikhasingrajput


C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
static int check357(int N)
{
    // Stores if a valid
    // combination exists
    int f = 0;
  
    // if N is of the form 3n
    if (N % 3 == 0)
        return (N / 3) * 100 + 0 * 10 + 0;
  
    else if (N % 3 == 1) {
  
        // if N is of the form 3n + 1
        if (N - 7 >= 0)
            return ((N - 7) / 3) * 100 + 0 * 10 + 1;
    }
    else
  
        // if N is of the form 3n + 2
        if (N - 5 >= 0)
        return ((N - 5) / 3) * 100 + 1 * 10 + 0;
  
    // If no valid combinations exists
    return -1;
}
  
// Driver code
public static void Main()
{
    int N = 10;
    Console.Write(check357(N));
}
}
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
0 2 0

时间复杂度: O(N 3 )
辅助空间: O(1)

有效的方法:给定的问题可以通过使用数学来解决。
因为每个数字都可以用 3 的倍数表示,如 3x、3x+1 或 3x+2。利用这个事实,我们可以说 5 可以用 3x+2 的形式表示,7 可以用 3x+1 的形式表示。
借助这一观察, N可以用以下3种方式表示:

  • 如果N的形式为3x ,则它可以表示为3x
  • 如果N的形式为3x + 1
    • 如果N > 7 ,则 N 可以表示为3*(x – 2) + 7因为7是相似的 3*2 + 1
    • 否则,如果N <= 7 ,则不能以给定的形式表示。
  • 如果N的形式为3n + 2
    • 如果N > 5 ,则 N 可以表示为3x + 5因为5是相似的 3*1 + 2
    • 否则,如果N <= 5 ,则不能以给定的形式表示。

下面是上述方法的实现:

C++

// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
int check357(int N)
{
    // Stores if a valid
    // combination exists
    int f = 0;
  
    // if N is of the form 3n
    if (N % 3 == 0)
        return (N / 3) * 100 + 0 * 10 + 0;
  
    else if (N % 3 == 1) {
  
        // if N is of the form 3n + 1
        if (N - 7 >= 0)
            return ((N - 7) / 3) * 100 + 0 * 10 + 1;
    }
    else
  
        // if N is of the form 3n + 2
        if (N - 5 >= 0)
        return ((N - 5) / 3) * 100 + 1 * 10 + 0;
  
    // If no valid combinations exists
    return -1;
}
  
// Driver code
int main()
{
    int N = 10;
    cout << check357(N);
  
    return 0;
}

Java

// Java implementation of the above approach
import java.util.*;
public class GFG
{
    
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
static int check357(int N)
{
    
    // Stores if a valid
    // combination exists
    int f = 0;
  
    // if N is of the form 3n
    if (N % 3 == 0)
        return (N / 3) * 100 + 0 * 10 + 0;
  
    else if (N % 3 == 1) {
  
        // if N is of the form 3n + 1
        if (N - 7 >= 0)
            return ((N - 7) / 3) * 100 + 0 * 10 + 1;
    }
    else
  
        // if N is of the form 3n + 2
        if (N - 5 >= 0)
        return ((N - 5) / 3) * 100 + 1 * 10 + 0;
  
    // If no valid combinations exists
    return -1;
}
  
// Driver code
public static void main(String args[])
{
    int N = 10;
    System.out.print(check357(N));
}
}
  
// This code is contributed by Samim Hossain Mondal.

Python3

# Python implementation of the above approach
  
# Function to check if a number can be
# represented as summation of multiples
# of 3, 5 and 7
def check357(N):
    
    # Stores if a valid
    # combination exists
    f = 0;
  
    # if N is of the form 3n
    if (N % 3 == 0):
        return (N // 3) * 100 + 0 * 10 + 0;
  
    elif (N % 3 == 1):
  
        # if N is of the form 3n + 1
        if (N - 7 >= 0):
            return ((N - 7) // 3) * 100 + 0 * 10 + 1;
    else:
        if(N - 5 >= 0):
            # if N is of the form 3n + 2
            return ((N - 5) // 3) * 100 + 1 * 10 + 0;
    # If no valid combinations exists
    return -1;
  
# Driver code
if __name__ == '__main__':
    N = 10;
    print(check357(N));
  
# This code is contributed by shikhasingrajput

C#

// C# implementation of the above approach
using System;
class GFG
{
// Function to check if a number can be
// represented as summation of multiples
// of 3, 5 and 7
static int check357(int N)
{
    // Stores if a valid
    // combination exists
    int f = 0;
  
    // if N is of the form 3n
    if (N % 3 == 0)
        return (N / 3) * 100 + 0 * 10 + 0;
  
    else if (N % 3 == 1) {
  
        // if N is of the form 3n + 1
        if (N - 7 >= 0)
            return ((N - 7) / 3) * 100 + 0 * 10 + 1;
    }
    else
  
        // if N is of the form 3n + 2
        if (N - 5 >= 0)
        return ((N - 5) / 3) * 100 + 1 * 10 + 0;
  
    // If no valid combinations exists
    return -1;
}
  
// Driver code
public static void Main()
{
    int N = 10;
    Console.Write(check357(N));
}
}
// This code is contributed by Samim Hossain Mondal.

Javascript



输出:
101

时间复杂度: O(1)
辅助空间: O(1)