📜  不使用循环和递归的数组元素的总和

📅  最后修改于: 2021-05-30 02:37:53             🧑  作者: Mango

给定N个元素的数组,任务是查找N个元素的总和,而无需使用循环(for,while和doWhile)和递归。
例子:

Input: arr[]={1, 2, 3, 4, 5}   
Output: 15

Input: arr[]={10, 20, 30}   
Output: 60

方法:无条件跳转语句可用于解决此问题。
无条件跳转语句:
跳转语句中断了语句的顺序执行,因此执行在程序的另一点继续进行。如果跳转目标超出其范围,则跳转会破坏自动变量。有四种导致C无条件跳转的语句:break,continue,goto和return。
要解决此特定问题,goto语句可能会很有用。
goto声明
goto语句是一个跳转语句,有时也称为无条件跳转语句。 goto语句可用于从函数的任何位置跳转到任何位置。
语法

Syntax1      |   Syntax2
----------------------------
goto label;  |    label:  
.                  |    .
.                  |    .
.                  |    .
label:          |    goto label;

在以上语法中,第一行告诉编译器转到或跳到标记为标签的语句。这里label是一个用户定义的标识符,它指示目标语句。 “ label:”之后紧随其后的语句是目标语句。 “标签:”也可以出现在“转到标签”之前;上面语法中的语句。

去

下面是上述方法的实现:

C++
// C++ program to find the sum of
// N elements with goto statement
 
#include 
using namespace std;
 
// Function to perform desired operation
int operate(int array[], int N)
{
    int sum = 0, index = 0;
 
label:
    sum += array[index++];
 
    if (index < N) {
 
        // backward jump of goto statement
        goto label;
    }
 
    // return the sum
    return sum;
}
 
// Driver Code
int main()
{
 
    // Get N
    int N = 5, sum = 0;
 
    // Input values of an array
    int array[] = { 1, 2, 3, 4, 5 };
 
    // Find the sum
    sum = operate(array, N);
 
    // Print the sum
    cout << sum;
}


C
// C program to find the sum of
// N elements with goto statement
 
#include 
 
// Function to perform desired operation
int operate(int array[], int N)
{
    int sum = 0, index = 0;
 
label:
    sum += array[index++];
 
    if (index < N) {
 
        // backward jump of goto statement
        goto label;
    }
 
    // return the sum
    return sum;
}
 
// Driver Code
int main()
{
 
    // Get N
    int N = 5, sum = 0;
 
    // Input values of an array
    int array[] = { 1, 2, 3, 4, 5 };
 
    // Find the sum
    sum = operate(array, N);
 
    // Print the sum
    printf("%d", sum);
}


Java
// Java program to find the sum of
// N elements
class GFG
{
  // Function to perform desired operation
  static int operate(int array[], int N)
  {
    int sum = 0, index = 0;      
    while(true)
    {
      sum += array[index++];      
      if (index < N)
      {
 
        // backward jump of goto statement
        continue;
      }
      else
      {
        break;
      }
    }
 
    // return the sum
    return sum;
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Get N
    int N = 5, sum = 0;
 
    // Input values of an array
    int array[] = { 1, 2, 3, 4, 5 };
 
    // Find the sum
    sum = operate(array, N);
 
    // Print the sum
    System.out.print(sum);
  }
}
 
// This code is contributed by divyeshrabaiya07


Python3
# Python3 program to find the sum of
# N elements
 
# Function to perform desired operation
def operate(array, N) :  
    Sum, index = 0, 0  
    while(True) :
        Sum += array[index]
        index += 1
        if index < N :
           
            # backward jump of goto statement
            continue
        else :
            break
         
    # return the sum
    return Sum
     
# Get N
N, Sum = 5, 0
 
# Input values of an array
array = [ 1, 2, 3, 4, 5 ]
 
# Find the sum
Sum = operate(array, N)
 
# Print the sum
print(Sum)
 
# This code is contributed by divyesh072019


C#
// C# program to find the sum of
// N elements with goto statement
using System;
 
class GFG
{
// Function to perform desired operation
static int operate(int[] array, int N)
{
    int sum = 0, index = 0;
 
label:
    sum += array[index++];
 
    if (index < N)
    {
 
        // backward jump of goto statement
        goto label;
    }
 
    // return the sum
    return sum;
}
 
// Driver Code
public static void Main()
{
 
    // Get N
    int N = 5, sum = 0;
 
    // Input values of an array
    int[] array = { 1, 2, 3, 4, 5 };
 
    // Find the sum
    sum = operate(array, N);
 
    // Print the sum
    Console.Write(sum);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
15
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”