在数组中打印中间值
给定一个整数数组,打印当前索引元素和数组的连续下一个索引元素之间的中间值。
例子:
Input : arr[] = { 4, 2, 7, 5};
Output :
Intermediate elements between 4 and 2
2 3 4
Intermediate elements between 2 and 7
2 3 4 5 6 7
Intermediate elements between 7 and 5
5 6 7
C++
// C++ program to print the
// intermediate value
#include
using namespace std;
void inter(int arr[], int n)
{
for (int l = 0; l < n - 1; l++)
{
// points to first index element
int i = arr[l];
// points to preceding index element
int j = arr[l + 1];
// Find big element
// between the above elements
int big = i > j ? i : j;
// Find small element
// between the above elements
int sml = i < j ? i : j;
cout<<"Intermediate elements between "<<
i <<" and "<
Java
// Java program to print the
// intermediate values
public class GFG
{
static void inter(int[] arr)
{
for (int l = 0; l < arr.length - 1; l++)
{
// points to first index element
int i = arr[l];
// points to preceding index element
int j = arr[l + 1];
// Find big element
// between the above elements
int big = i > j ? i : j;
// Find small element
// between the above elements
int sml = i < j ? i : j;
System.out.println("Intermediate elements between "
+ i + " and " + j);
for (int k = sml; k <= big; k++)
System.out.print(k + " ");
System.out.println();
}
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 4, 2, 7, 5 };
inter(arr);
}
}
Python3
# Python 3 program to print the
# intermediate value
def inter(arr, n) :
for l in range( 0, n - 1) :
# points to first index element
i = arr[l]
# points to preceding index element
j = arr[l + 1]
# Find big element
# between the above elements
if(i>j) :
big = i
else :
big = j
# Find small element
# between the above elements
if(i
C#
// C# program to print the
// intermediate values
using System;
public class GFG
{
static void inter(int[] arr)
{
for (int l = 0; l < arr.Length - 1; l++)
{
// points to first index element
int i = arr[l];
// points to preceding index element
int j = arr[l + 1];
// Find big element
// between the above elements
int big = i > j ? i : j;
// Find small element
// between the above elements
int sml = i < j ? i : j;
Console.WriteLine("Intermediate elements between "
+ i + " and " + j);
for (int k = sml; k <= big; k++)
Console.Write(k + " ");
Console.WriteLine();
}
}
// Driver code
public static void Main()
{
int[] arr = { 4, 2, 7, 5 };
inter(arr);
}
}
// This code is contributed by vt_m.
PHP
$j ? $i : $j;
// Find small element
// between the above elements
$sml = $i < $j ? $i : $j;
echo "interermediate elements between ",
$i ," and ",$j,"\n";
for ($k = $sml; $k <= $big; $k++)
echo $k," ";
echo "\n";
}
}
// Driver Code
$arr = array(4, 2, 7, 5);
$n=count($arr);
inter($arr,$n);
// This code is contributed by anuj_67.
?>
Javascript
输出:
Intermediate elements between 4 and 2
2 3 4
Intermediate elements between 2 and 7
2 3 4 5 6 7
Intermediate elements between 7 and 5
5 6 7