📌  相关文章
📜  给定数组的奇数索引中所有组合数的总和

📅  最后修改于: 2021-06-27 00:22:23             🧑  作者: Mango

给定大小为N的数组arr [] ,其中包含至少一个复合数字。任务是找出数组中所有处于奇数索引(其中索引基于1)的复合数字的总和。

例子:

方法:

为了解决上述问题,主要思想是检查复合数字,即,如果整数有两个以上的因子,则它是一个复合数字,否则就没有。然后,我们将迭代并检查数组的奇数索引中的复合数。如果它是一个复合数,则将其添加到sum变量中,否则将其跳过,直到到达数组末尾为止。

下面是上述方法的实现:

C++
// C++ implementation to find the sum of all the
// composite numbers from odd indices of the given array
#include
using namespace std;
  
// Function to check for composite numbers
int composite(int n){
    int flag = 0; 
    
    int c = 0;
      
    // Check if the factors are greater than 2
    for (int j = 1; j <= n; j++){
      if (n % j == 0){
            c += 1;  
      }    
    }         
             
    // Check if the number is composite or not
    if (c >= 3)
      flag = 1;
          
    return flag;
}
      
// Function to print the sum of all 
// composite numbers in the array
void odd_indices(int arr[],int n){
      
int sum = 0;
      
// Iterate for odd indices in the array
for (int k = 0; k < n; k += 2){
    int check = composite(arr[k]);
          
    // Check if the number is composite
    // then add it to sum
    if (check == 1)
        sum += arr[k];
}
          
    // return the sum
    cout << sum << endl;
}
  
// Driver code 
int main(){
    int arr[] = {13, 5, 8, 16, 25};
    int n = sizeof(arr)/sizeof(arr[0]);
    odd_indices(arr,n);
}
  
// This code is contributed by Surendra_Gangwar


Java
// Java implementation to find the sum of all the
// composite numbers from odd indices of the given array
  
class GFG{
  
// Function to check for composite numbers
static int composite(int n){
    int flag = 0;
      
    int c = 0;
      
    // Check if the factors are greater than 2
    for (int j = 1; j <= n; j++){
    if (n % j == 0){
            c += 1; 
    } 
    }         
          
    // Check if the number is composite or not
    if (c >= 3)
    flag = 1;
          
    return flag;
}
      
// Function to print the sum of all 
// composite numbers in the array
static void odd_indices(int arr[],int n){
      
int sum = 0;
      
// Iterate for odd indices in the array
for (int k = 0; k < n; k += 2){
    int check = composite(arr[k]);
          
    // Check if the number is composite
    // then add it to sum
    if (check == 1)
        sum += arr[k];
}
          
    // return the sum
    System.out.print(sum +"\n");
}
  
// Driver code 
public static void main(String[] args){
  
    int arr[] = {13, 5, 8, 16, 25};
    int n = arr.length;
    odd_indices(arr,n);
}
}
// This code contributed by sapnasingh4991


Python3
# Python3 implementation to find the sum of all the
# composite numbers from odd indices of the given array
  
# Function to print the sum of all 
# composite numbers in the array
def odd_indices(arr):
      
    sum = 0
      
    # Iterate for odd indices in the array
    for k in range (0, len(arr), 2):
          
        check = composite (arr[k])
          
        # Check if the number is composite
        # then add it to sum
        sum += arr[k] if check == 1 else 0
          
    # return the sum
    print (sum)
  
# Function to check for composite numbers
def composite(n):
      
    flag = 0
      
    c = 0 
      
    # Check if the factors are greater than 2
    for j in range (1, n + 1):
          
        if (n % j == 0):
              
            c += 1
      
    # Check if the number is composite or not
    if (c >= 3):
        flag = 1
          
    return flag
  
# Driver code 
if __name__ == "__main__": 
      
    arr = [13, 5, 8, 16, 25] 
  
    odd_indices(arr)


C#
// C# implementation to find the sum 
// of all the composite numbers from 
// odd indices of the given array
using System;
  
class GFG{
  
// Function to check for composite numbers
static int composite(int n)
{
    int flag = 0;
    int c = 0;
      
    // Check if the factors are greater than 2
    for(int j = 1; j <= n; j++)
    {
       if (n % j == 0)
       {
           c += 1; 
       } 
    }         
          
    // Check if the number is composite or not
    if (c >= 3)
        flag = 1;
    return flag;
}
      
// Function to print the sum of all 
// composite numbers in the array
static void odd_indices(int []arr,int n)
{
    int sum = 0;
      
    // Iterate for odd indices in the array
    for(int k = 0; k < n; k += 2)
    {
       int check = composite(arr[k]);
         
       // Check if the number is composite
       // then add it to sum
       if (check == 1)
           sum += arr[k];
    }
      
    // return the sum
    Console.Write(sum + "\n");
}
  
// Driver code 
public static void Main(String[] args)
{
    int []arr = { 13, 5, 8, 16, 25 };
    int n = arr.Length;
      
    odd_indices(arr, n);
}
}
  
// This code is contributed by Rohit_ranjan


输出:
33

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。