본문 바로가기

알고리즘 문제 풀이/Programmers

자연수 뒤집어 배열로 만들기 - 1단계 (C, C++)

반응형
SMALL
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int* solution(long long n) {
	// 리턴할 값은 메모리를 동적 할당해주세요.
    int* answer = (int*)malloc(sizeof(int) * 100);
    int i = 0;
    while (n) {
    	answer[i] = n % 10; # 1의 자리 수를 넣는다.
        n /= 10; # 1의 자리 수를 없앤다.
        i++;
    }
    return answer;
}

#include <string>
#include <vector>

using namespace std;

vecotr<int> solution(long long n) {
	vector<int> answer;
    int temp;
    while (n) {
    	temp = n % 10;
        n /= 10;
        answer.push_back(temp);
    }
    return answer;
}
반응형
LIST