ในบทความนี้ เราจะเข้าใจวิธีการหาผลรวมของตัวเลข N โดยใช้การเรียกซ้ำ ฟังก์ชันแบบเรียกซ้ำคือฟังก์ชันที่เรียกตัวเองหลายครั้งจนกระทั่งตรงตามเงื่อนไขที่กำหนด
การเรียกซ้ำเป็นกระบวนการของการทำซ้ำรายการในลักษณะที่คล้ายคลึงกัน ในภาษาโปรแกรม หากโปรแกรมอนุญาตให้คุณเรียกใช้ฟังก์ชันภายในฟังก์ชันเดียวกันได้ จะเรียกว่าการเรียกใช้ฟังก์ชันแบบเรียกซ้ำ
ภาษาโปรแกรมหลายภาษาใช้การเรียกซ้ำโดยใช้สแต็ค โดยทั่วไป เมื่อใดก็ตามที่ฟังก์ชัน (ผู้โทร) เรียกใช้ฟังก์ชันอื่น (callee) หรือเรียกตัวเองว่าเป็นผู้รับสาย ฟังก์ชันผู้โทรจะโอนการควบคุมการดำเนินการไปยังผู้รับสาย ขั้นตอนการโอนนี้อาจเกี่ยวข้องกับข้อมูลบางส่วนที่ต้องส่งผ่านจากผู้โทรไปยังผู้รับสาย
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
Enter the value of N : 6 Enter the elements of array : 15 30 45 80 100 140
ผลผลิต
ผลลัพธ์ที่ต้องการจะเป็น −
The total of N numbers is : 410
อัลกอริทึม
Step 1 - START Step 2 - Declare two integer values namely N , my_sum and i and an integer array ‘my_array’ Step 3 - Read the required values from the user/ define the values Step 4 - A recursive function ‘RecursiveSum is defined which takes two integers as input. The function computes the reminder by re-iterating over the function multiple times, until the base condition is reached. Step 5 - The recursive function ‘RecursiveSum is called and its result is stored Step 6 - Display the result Step 7 - Stop
ตัวอย่างที่ 1
ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์เขียนโค้ดของเราได้
.
import java.util.Scanner;
public class ArraySum {
public static int RecursiveSum(int my_array[], int i,int N){
if (i == N)
return 0;
return my_array[i] + RecursiveSum(my_array, i + 1,N);
}
public static void main(String[] args){
int N, my_sum, i;
N = 6;
my_sum = 0;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the value of N : ");
N = my_scanner.nextInt();
int my_array[] = new int[N];
System.out.println("Enter the elements of the array :" );
for ( i = 0 ; i < N ; i++ ){
my_array[i] = my_scanner.nextInt();
}
my_sum = RecursiveSum(my_array, 0, N);
System.out.println("\n The total of N numbers is : " + my_sum);
}
} ผลลัพธ์
Required packages have been imported A reader object has been defined Enter the value of N : 6 Enter the elements of the array : 15 30 45 80 100 140 The total of N numbers is : 410
ตัวอย่างที่ 2
ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล
public class Main {
public static void main(String[] args) {
int[] my_array = {15, 20, 25, 30, 35, 40};
int my_input , i, array_size;
array_size = 5;
my_input = 25;
boolean my_check = false;
System.out.println("The number is defined as " +my_input);
System.out.println("The elements in the integer array is defined as :" );
for ( i = 0 ; i < array_size ; i++ ){
System.out.print(my_array[i] +" ");
}
for ( i = 0 ; i < array_size ; i++ ) {
if (my_array[i] == my_input) {
my_check = true;
break;
}
}
if(my_check)
System.out.println("\nThe array contains the given value");
else
System.out.println("\nThe array doesnot contain the given value");
}
} ผลลัพธ์
The number is defined as 25 The elements in the integer array is defined as : 15 20 25 30 35 The array contains the given value