问:打印数组奇数位置元素的程序。

17 Mar 2025 | 阅读 2 分钟

说明

在这个程序中,我们需要打印数组中位于奇数位置的元素。这可以通过循环遍历数组并打印数组的元素来实现,通过将i递增2直到到达数组的末尾。

Program to convert a given binary tree to doubly linked list

在上述数组中,位于奇数位置的元素是 a、c 和 e。

算法

  1. 声明并初始化一个数组。
  2. 计算已声明数组的长度。
  3. 通过将变量“i”初始化为 0,然后每次递增 2(即 i=i+2)来循环遍历数组。
  4. 打印位于奇数位置的元素。

解决方案

Python

输出

Elements of given array present on odd position: 
1
3
5

C

输出

Elements of given array present on odd position: 
1
3
5

JAVA

输出

Elements of given array present on odd position: 
1
3
5

C#

输出

Elements of given array present on odd position: 
1
3
5

PHP

输出

Elements of given array present on odd position: 
1
3
5
 
下一主题#