57번 문제

내가 풀이한 답
Stack 구조를 활용하여 2로 나눈 몫이 1이면 몫과 나머지 순으로 출력하고 1이 아니면 다시 재귀함수를 호출하였다.
//#include <bits/stdc++.h> #include <iostream> using namespace std; void binary(int n){ if(n/2==1) { cout << n/2 << n%2; return; } else { binary(n/2); cout << n%2; } } int main() { //freopen("input.txt", "rt", stdin); ios_base::sync_with_stdio(false); int n; cin >> n; binary(n); return 0; }
결과는 통과하였다.

사이트의 답안
#include<stdio.h> #include<vector> #include<algorithm> using namespace std; void D(int x){ if(x==0) return; else{ D(x/2); printf("%d", x%2); } } int main(){ freopen("input.txt", "rt", stdin); int n; scanf("%d", &n); D(n); return 0; }
사이트의 답과 비슷했다.