SW 꿈나무
[Level 1][C] 자연수 뒤집어 배열로 만들기 본문
문제
자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해 주세요.
예들 들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.
제한 조건
n은 10,000,000,000 이하인 자연수입니다.
Example
Input 1 : 12345
Output 1 : [5,4,3,2,1]
Code
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> int* solution(long long n) { long long digit=n; // digit 선언 및 n 대입 int count=1; // count 선언 및 1 대입 -> 아래 조건문에서 10이하의 수는 count 되지 않기 때문 for(int i=0;i<10;i++) { // 제한 조건인 의 승수만큼 반복 if(digit/10>0) { // digit / 10 이 0보다 크면 digit/=10; // digit = digit / 10 count+=1; // count = count + 1 } } int* answer = (int*)malloc(sizeof(int)*count); // answer 에 count 수만큼 메모리 동적 할당 for(int i=0;i<count;i++) { // count 수만큼 반복 answer[i]=n%10; // answer[i] 자리에 n % 10 대입 n=n/10; // n = n / 10 } return answer; // answer 반환 }
'Algorithm > Programmers' 카테고리의 다른 글
[Level 1][C] 이상한 문자 만들기 (0) | 2020.03.26 |
---|---|
[Level 1][C] 자릿수 더하기 (0) | 2020.03.26 |
[Level 1][C] 정수 제곱근 판별 (0) | 2020.03.26 |
[Level 1][C] 짝수와 홀수 (0) | 2020.03.26 |
[Level 1][C] 핸드폰 번호 가리기 (0) | 2020.03.26 |
Comments