UVA 993 Product of digits
Topic: Given a number n, requires a number, so that the number of each bit will be equal to N, and the value of the number is the smallest.
Problem solving: A simple decomposition factor. Note that there are two points: 1) factor from 9 to 2 times, can guarantee the minimum number of bits, 2) when n equals 1 is the best special treatment.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace Std;int N, dig[100000];int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n); int flag = 0, cnt = 9, Cnt2 = 0;if (n = = 1) {printf ("1\n"); continue;} while (n! = 1) {if (cnt = = 2 && n% cnt! = 0) {flag = 0;break;} if (n cnt! = 0) {cnt--;} else {flag = 1;n/= cnt;dig[cnt2++] = cnt;cnt = 9;}} if (!flag) printf (" -1\n"), else {for (int i = cnt2-1; I >= 0; i--) {printf ("%d", Dig[i]);} printf ("\ n");}} return 0;}
UVA 993 Product of digits (decomposition factor)