배열은 데이터 유형이 동일한 여러 요소의 컨테이너입니다. 요소의 인덱스는 0부터 시작합니다. 즉, 첫 번째 요소의 인덱스는 0입니다.
이 문제에서는 두 개의 짝수 인덱스 숫자와 두 개의 홀수 인덱스 숫자 사이의 절대 차이를 찾아야 합니다.
짝수 인덱스 번호 =0,2,4,6,8….
홀수 색인 번호 =1,3,5,7,9…
절대 차이는 두 요소 간의 차이 계수입니다.
예를 들어,
15와 7의 절대 차이 =(|15 - 7|) =8
Input: arr = {1 , 2, 4, 5, 8}
Output :
Absolute difference of even numbers = 4
Absolute difference of odd numbers = 3 설명
짝수 요소는 1, 4,8입니다.
절대적인 차이는
(|4 - 1|) =3 및 (|8 - 4|) =4
홀수 요소는 2,5입니다.
절대적인 차이는
(|5- 2|) =3
예시
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[] = { 1, 5, 8, 10, 15, 26 };
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"The array is : \n";
for(int i = 0;i < n;i++){
cout<<" "<<arr[i];
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
even = abs(even - arr[i]);
else
odd = abs(odd - arr[i]);
}
cout << "Even Index absolute difference : " << even;
cout << endl;
cout << "Odd Index absolute difference : " << odd;
return 0;
}
} 출력
The array is : 1 5 8 10 15 26 Even index absolute difference : 8 Odd index absolute difference : 21