| 
        
         | 
       
      
        | 
        
         | 
       
      
         | 
       
	    
          | 
       
      
        | 
      
자료구조 - 순환과 반복
      
	 | 
       
      
         | 
       
      
      
         | 
       
      
          | 
       
      
        자료구조 - 순환과 반복 
 
1. 팩토리얼계산 
 
◎순환 
#include [iostream] 
using namespace std; 
 
int factorial(int n) 
{ 
 if(n==1) 
 return 1; 
 else 
 return (n * factorial(n-1)); 
} 
void main() 
{ 
 int n; 
 cout [[ 정수 입력 : ; 
 cin ]] n; 
 cout [[ n [[ 팩토리얼 계산 : [[ factorial(n) [[ endl; 
} 
 
◎반복 
#include [iostream] 
using namespace std; 
 
int factorial_iter(int n) 
{ 
 int k, v=1; 
 for(k=n; k]0; k--) 
 v = v * k; 
 return v; 
} 
 
void main() 
{ 
 int n; 
 cout [[ 정수 입력 : ; 
 cin ]] n; 
 cout [[ n [[ 팩토리얼 계산 : [[ factorial_iter(n) [[ endl; 
} 
 
2. 예제프로그램 함수p() 
 
◎ 순환 
#include [iostream] 
using namespace std; 
 
void p(int n) 
{ 
 if(n]0) 
 { 
 p(n-2); 
 cout [[ n; 
 p(n-1); 
 } 
} 
 
int main() 
{ 
 p(4); 
 return 0; 
} 
 
3.최대 공약수 
 
◎ Recursive version 
#include [iostream] 
using namespace std; 
 
int gcd(int x,int y) 
{ 
 return y  gcd(y,x%y) : x ; 
} 
 
void main() 
{ 
 int x,y; 
 cout [[ 정수 2개 입력 : ; 
 cin ]] x ]] y; 
.... | 
       
      
      
      
         | 
       
      
         | 
       
      
         | 
       
      
          | 
       
      
        | 
			  
		     | 
       
      
         | 
       
      
         | 
       
      
        | 
        
         | 
       
	
    	| 
        
         | 
           
      
         | 
       
      
         | 
       
      
         | 
       
      
        |   | 
       
      | 
     | 
    
 |