알고리즘 25 - Reversed sequence

tamagoyakii·2021년 10월 8일
0

알고리즘

목록 보기
25/89

Q.

Build a function that returns an array of integers from n to 1 where n>0.

Example : n=5 --> [5,4,3,2,1]

A)

#include <stdlib.h>

unsigned short *reverse_seq(unsigned short num)
{
  if (num == 0)
    return 0;
  unsigned short *arr = malloc(sizeof(unsigned short) * num);
  unsigned short n = num;
  for (int i = 0; i < num; i++)
    arr[i] = n--;
  return arr;
}

0개의 댓글