Medium

Given an integer product, find the smallest positive (i.e. greater than 0) integer the product of whose digits is equal to product. If there is no such integer, return -1 instead.

Example

  • For product = 12, the output should be
    digitsProduct(product) = 26;
  • For product = 19, the output should be
    digitsProduct(product) = -1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer product

    Guaranteed constraints:
    0 ≤ product ≤ 600.

  • [output] integer

[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

int digitsProduct(int product)
{
	for(int i=product;i<=5555;i++)
	{
		int temp=i, mul=1;

		while(temp>0)
		{
			mul*=temp%10;
			temp/=10;
		}

		if(mul==product)
			return i;
	}

	return -1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> messageFromBinaryCode  (0) 2020.04.06
<Codesignal> File Naming  (0) 2020.04.06
<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06
<Codesignal> Valid Time  (0) 2020.04.06

+ Recent posts