배열에 L, R, P를 입력으로 하고 모듈로 아래의 곱을 출력으로 하여 L과 R 사이의 범위를 찾아 표시하는 작업입니다.
그림과 같이 요소의 배열이 있고 왼쪽 값인 L이 2이고 오른쪽 값인 R이 2입니다. 이제 프로그램은 이들 사이의 범위의 곱을 찾아야 합니다.

예시
Input-: A[] = { 1, 2, 3, 4, 5, 6 }
P = 29 L = 2 R = 6
Output-: 24
Input-: A[] = {1, 2, 3, 4, 5, 6},
L = 2 R = 5 P = 113
Output-: 7 아래 프로그램에서 사용된 접근 방식은 다음과 같습니다. -
- 정수 요소, 왼쪽 값(L), 오른쪽 값(R) 및 P(소수 값)의 배열에 입력을 가져옵니다.
- 왼쪽 값에서 오른쪽 값으로 요소 순회 시작
- 임시 변수에 곱셈을 계속 저장
- 소수 값으로 모듈로 연산을 계속 수행
- 최종 결과 인쇄
알고리즘
Start
Step 1 -> declare function to calculate product
int calculateProduct(int A[], int L,int R, int P)
declare variable as int i
set L = L – 1
set R = R – 1
declare int ans = 1
Loop For i = L and i <= R and i++
Set ans = ans * A[i]
Set ans = ans % P
End
return ans
Step 2-> In main()
Declare an array as int A[] = { 1, 2, 3, 4, 5, 6 }
Declare variable as int P = 29
Declare variable as int L = 2, R = 6
Print A, L, R, P
Stop 예시
#include <stdio.h>
int calculateProduct(int A[], int L,int R, int P) {
int i;
//Because array starts with 0 and
//L R starts from 1.
L = L - 1;
R = R - 1;
int ans = 1;
for ( i = L; i <= R; i++) {
ans = ans * A[i];
ans = ans % P;
}
return ans;
}
int main() {
int A[] = { 1, 2, 3, 4, 5, 6 };
int P = 29;
int L = 2, R = 6;
printf("%d\n", calculateProduct(A, L, R, P));
return 0;
} 출력
24