https://www.acmicpc.net/problem/1629

Baekjoon Online JudgeBaekjoon Online Judgewww.acmicpc.net

이 문제 풀었는데 아래 첫번ㅉㅐ 코드로 하니까 틀렸고 그래서 두 번째 코드로 풀었는데 맞더라구 두 코드 차이가 뭐야? 내가 봤을 때 똑같아 보여 ㅠㅠ


첫 번째 코드

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19#include<iostream> using namespace std; int a, b, c; int func(int x) { if (x == 1) return a % c; int res = func(x / 2) % c; if (x % 2 == 0) return res * res ; else return res * res * (a % c); } int main() { cin >> a >> b >> c; cout << func(b); }


두 번째 코드

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19#include<iostream> using namespace std; int a, b, c; int func(int x) { if (x == 1) return a % c; int res = func(x / 2); if (x % 2 == 0) return res * res % c; else return res * res * a % c; } int main() { cin >> a >> b >> c; cout<< func(b); }