UVa 993 - Product of digits

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

greedy,從 9 開始遞減,只要可以整除 n ,就加進答案內 。
最後再從小的開始輸出即可。

Code

UVa 993UVa 993 - Product of digits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>

int main()
{
int Case;
scanf("%d", &Case);
while (Case--)
{
int n;
scanf("%d", &n);
if (n == 1)
{
puts("1");
continue;
}

int num[10] = { 0, 1 }, i, j;
for (i = 9; n > 1 && i > 1; --i)
{
while (!(n%i))
{
++num[i];
n /= i;
}
}

if (n != 1)
puts("-1");
else
{
for (i = 2; i < 10; i++)
for (j = 0; j < num[i]; j++)
putchar('0' + i);
putchar('\n');
}
}


return 0;
}