logo

C에서 POW 기능을 사용하지 않고 전력 알아보기

pow() 함수는 주어진 정수의 거듭제곱을 계산하는 데 사용됩니다. 이제 이 기사에서는 C에서 pow() 함수를 사용하지 않고 정수의 거듭제곱을 계산하는 방법을 프로그램의 도움으로 이해하겠습니다.

주어진 정수의 거듭제곱을 결정하기 위해 for 루프 사용하기

^ b를 찾아야 한다고 상상해 보십시오. 가장 쉬운 방법은 루프를 사용하여 a에 b를 곱하는 것입니다.

  • a ^ b를 입력으로 둡니다. 밑은 a이고 지수는 b입니다.
  • 1의 거듭제곱으로 시작합니다.
  • 루프를 사용하여 다음 명령을 b번 실행합니다.
  • 전력 = 전력 * a
  • 전력 시스템에는 최종 솔루션인 a ^ b가 있습니다.

C 프로그램의 예를 통해 위의 접근 방식을 더 잘 이해해 보겠습니다.

수학.랜덤 자바
 # include # include # include # include # include int Pow ( int a , int b ) { int power = 1 , i ; for ( i = 1 ; i <= b ; + i ) { power="power" * a } return int main ( long base , exponent printf ' enter : scanf % d & ^ pow < pre> <p> <strong>Output:</strong> </p> <pre> Enter Base: 5 Enter Power: 3 5 ^ 3 = 125 .......................... Process executed in 3.22 seconds Press any key to continue. </pre> <p> <strong>Explanation</strong> </p> <p>The code above has an O (N) time complexity, where N is the exponent. O is the space complexity (1).</p> <h3>Using While loop:</h3> <pre> # include # include # include # include # include int main ( ) { int n , exp , exp1 ; long long int value = 1 ; printf ( &apos; enter the number and its exponential :  n  n &apos; ) ; scanf ( &apos; % d % d &apos; , &amp; n , &amp; exp ) ; exp1 = exp ; // storing original value for future use // same as while ( ( - - exp ) ! = - 1 ) while ( exp - - &gt; 0 ) { value * = n ; // multiply n to itself exp times } printf ( &apos;  n  n % d ^ % d = % l l d  n  n &apos; , n , exp1 , value ) ; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. </pre> <p> <strong>Explanation</strong> </p> <p>Long Long Int is twice as large as Long Int. The format specifier for long long int is percent lld.</p> <h2>Using Recursion to find the Power of Given Integer</h2> <p>Assume that a ^ b is the input. The power of &apos;a&apos; will increase by one with each recursive call. To obtain a ^ b, we call the recursive function b twice.</p> <ul> <li>Let Pow ( a, b ) be the recursive function used to calculate a ^ b.</li> <li>Simply return 1 if b == 0; else, return Pow (a, b -1) * a.</li> </ul> <p> <strong>Let&apos;s understand the above approach better with an example of a program in C:</strong> </p> <pre> # include # include # include # include # include int Pow ( int a , int b ) { if ( b = = 0 ) return 1 ; else return Pow ( a , b - 1 ) * X ; } int main ( ) { long long int base , exponent ; printf ( &apos; enter Base : &apos; ) ; scanf ( &apos; % d &apos; , &amp; base ) ; printf ( &apos; enter Power : &apos; ) ; scanf ( &apos; % d &apos; , &amp; exponent ) ; printf ( &apos; % d ^ % d = % d &apos; , base , exponent , Pow ( base , exponent ) ) ; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. </pre> <p> <strong>Explanation:</strong> </p> <p>In the above example of a code in C, time complexity would be exponent N, O(N) &amp; O(N) space complexity, internal stack.</p> <hr></=>

설명

위의 코드는 O(N) 시간 복잡도를 가지며, 여기서 N은 지수입니다. O는 공간 복잡도(1)입니다.

While 루프 사용:

 # include # include # include # include # include int main ( ) { int n , exp , exp1 ; long long int value = 1 ; printf ( &apos; enter the number and its exponential :  n  n &apos; ) ; scanf ( &apos; % d % d &apos; , &amp; n , &amp; exp ) ; exp1 = exp ; // storing original value for future use // same as while ( ( - - exp ) ! = - 1 ) while ( exp - - &gt; 0 ) { value * = n ; // multiply n to itself exp times } printf ( &apos;  n  n % d ^ % d = % l l d  n  n &apos; , n , exp1 , value ) ; return 0; } 

산출:

 enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. 

설명

마두리가 어서 오라고 했어

Long Long Int는 Long Int의 두 배입니다. long long int의 형식 지정자는 퍼센트 lld입니다.

재귀를 사용하여 주어진 정수의 거듭제곱 구하기

a ^ b가 입력이라고 가정합니다. 각 재귀 호출마다 'a'의 거듭제곱이 1씩 증가합니다. a ^b를 얻으려면 재귀 함수 b를 두 번 호출합니다.

  • Pow(a,b)를 a^b를 계산하는 데 사용되는 재귀 함수로 설정합니다.
  • b == 0이면 간단히 1을 반환합니다. 그렇지 않으면 Pow (a, b -1) * a를 반환합니다.

C 프로그램의 예를 통해 위의 접근 방식을 더 잘 이해해 보겠습니다.

 # include # include # include # include # include int Pow ( int a , int b ) { if ( b = = 0 ) return 1 ; else return Pow ( a , b - 1 ) * X ; } int main ( ) { long long int base , exponent ; printf ( &apos; enter Base : &apos; ) ; scanf ( &apos; % d &apos; , &amp; base ) ; printf ( &apos; enter Power : &apos; ) ; scanf ( &apos; % d &apos; , &amp; exponent ) ; printf ( &apos; % d ^ % d = % d &apos; , base , exponent , Pow ( base , exponent ) ) ; return 0; } 

산출:

 Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. 

설명:

위의 C 코드 예에서 시간 복잡도는 지수 N, O(N) 및 O(N) 공간 복잡도, 내부 스택입니다.