原文:

给定两个整数 lr ,任务是使用打印从 lr 的所有偶数和奇数。

示例:

输入: l = 1,r = 10 输出: 偶数:2 4 6 8 10 奇数:1 3 5 7 9

输入: l = 10,r = 25 输出: 偶数:10 12 14 16 18 20 22 24 奇数:11 13 15 17 19 21 23 25

方法:按照以下步骤使用解决问题:

  • 遍历【r,l】范围。
  • 使用以下递归关系,使用打印范围内的奇数元素:

奇数(l,r) = r % 2 == 1?奇数(左,右–2):奇数(左,右–1)

  • 使用以下使用递归打印范围内的偶数元素:

偶数(l,r) = r % 2 == 0?偶数(l,r–2):偶数(l,r–1)

下面是上述方法的实现:

c

// c   program to implement
// the above approach
#include 
using namespace std;
// function to print all the
// even numbers from l to r
void even(int l, int r)
{
    // base case
    if (r < l) {
        return;
    }
    // recurrence relation
    r % 2 == 0 ? even(l, r - 2)
               : even(l, r - 1);
    // check if r is even
    if (r % 2 == 0) {
        cout << r << " ";
    }
}
// function to print all the
// odd numbers from l to r
void odd(int l, int r)
{
    // base case
    if (r < l) {
        return;
    }
    // recurrence relation
    r % 2 == 1 ? odd(l, r - 2)
               : odd(l, r - 1);
    // check if r is even
    if (r % 2 == 1) {
        cout << r << " ";
    }
}
// driver code
int main()
{
    int l = 10, r = 25;
    cout << "even numbers:";
    // print all the
    // even numbers
    even(l, r);
    cout << endl;
    // print all the
    // odd numbers
    cout << "odd numbers:";
    odd(l, r);
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to implement
// the above approach
import java.util.*;
class gfg{
// function to print
// all the even numbers
// from l to r
static void even(int l,
                 int r)
{
  // base case
  if (r < l)
  {
    return;
  }
  // recurrence relation
  if(r % 2 == 0 )
    even(l, r - 2);
  else
    even(l, r - 1);
  // check if r is even
  if (r % 2 == 0)
  {
    system.out.print(r   " ");
  }
}
// function to print
// all the odd numbers
// from l to r
static void odd(int l,
                int r)
{
  // base case
  if (r < l)
  {
    return;
  }
  // recurrence relation
  if(r % 2 == 1 )
    odd(l, r - 2);
  else
    odd(l, r - 1);
  // check if r is even
  if (r % 2 == 1)
  {
    system.out.print(r   " ");
  }
}
// driver code
public static void main(string[] args)
{
  int l = 10, r = 25;
  system.out.print("even numbers:");
  // print all the
  // even numbers
  even(l, r);
  system.out.println();
  // print all the
  // odd numbers
  system.out.print("odd numbers:");
  odd(l, r);
}
}
// this code is contributed by rajput-ji

python 3

# python3 program to implement
# the above approach
# function to print all the
# even numbers from l to r
def even(l, r):
    # base case
    if (r < l):
        return
    # recurrence relation
    if (r % 2 == 0):
        even(l, r - 2)
    else:
        even(l, r - 1)
    # check if r is even
    if (r % 2 == 0):
        print(r, end = " ")
# function to print all the
# odd numbers from l to r
def odd(l, r):
    # base case
    if (r < l):
        return
    # recurrence relation
    if (r % 2 == 1):
        odd(l, r - 2)
    else:
        odd(l, r - 1)
    # check if r is even
    if (r % 2 == 1):
        print(r, end = " ")
# driver code
if __name__ == '__main__':
    l = 10
    r = 25
    print("even numbers:")
    # print all the
    # even numbers
    even(l, r)
    print()
    # print all the
    # odd numbers
    print("odd numbers:")
    odd(l, r)
# this code is contributed by amit katiyar

c

// c# program to implement
// the above approach
using system;
class gfg{
// function to print
// all the even numbers
// from l to r
static void even(int l,
                 int r)
{
  // base case
  if (r < l)
  {
    return;
  }
  // recurrence relation
  if(r % 2 == 0 )
    even(l, r - 2);
  else
    even(l, r - 1);
  // check if r is even
  if (r % 2 == 0)
  {
    console.write(r   " ");
  }
}
// function to print
// all the odd numbers
// from l to r
static void odd(int l,
                int r)
{
  // base case
  if (r < l)
  {
    return;
  }
  // recurrence relation
  if(r % 2 == 1 )
    odd(l, r - 2);
  else
    odd(l, r - 1);
  // check if r is even
  if (r % 2 == 1)
  {
    console.write(r   " ");
  }
}
// driver code
public static void main(string[] args)
{
  int l = 10, r = 25;
  console.write("even numbers:");
  // print all the
  // even numbers
  even(l, r);
  console.writeline();
  // print all the
  // odd numbers
  console.write("odd numbers:");
  odd(l, r);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

even numbers:10 12 14 16 18 20 22 24 
odd numbers:11 13 15 17 19 21 23 25

时间复杂度:o(n) t5辅助空间:** o(1)