993-product of digits

Source: Internet
Author: User


Question: 993-product of digits


A number is converted into the smallest number composed of factors by means of factorization.


Solution: to form a number, the factor only needs to start from 2 to 9, and because the minimum number is required, the factorization starts from the maximum factor, so the number of digits is the least, finally, the output is output from the smallest factor to ensure the minimum. 1 requires special determination, because all numbers have a factor of 1.


Code:

#include <stdio.h>#include <string.h>const int N = 10;int c[N];int n;bool factor() {memset (c, 0, sizeof (c));for (int i = N - 1; i >= 2; i--) {if (n == 1)return true;while (n % i == 0) {c[i]++;n /= i;}}if (n == 1)return true;return false;}int main () {int t;int c6;scanf ("%d", &t);while (t--) {scanf ("%d", &n);if (n != 1) {if (!factor())printf ("-1\n");else {for (int i = 2; i < 10 ; i++) for (int j = 0; j < c[i]; j++)printf ("%d", i);printf ("\n");}} else printf ("1\n");}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.