Easy

Determine if the given number is a power of some non-negative integer.

Example

  • For n = 125, the output should be
    isPower(n) = true;
  • For n = 72, the output should be
    isPower(n) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 ≤ n ≤ 400.

  • [output] boolean

    • true if n can be represented in the form a^b (a to the power of b) where a and b are some non-negative integers and b ≥ 2, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isPower(int n)
{
	if(n==1)
		return true;

	for(int i=2;i<=20;i++)
		for(int j=2;(int)pow(i,j)<=n;j++)
			if((int)pow(i,j)==n)
				return true;

	return false;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Square Digits Sequence  (0) 2020.04.15
<Codesignal> Is Sum of Consecutive 2  (0) 2020.04.15
<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14
<Codesignal> Is Smooth?  (0) 2020.04.14

+ Recent posts