-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmallest_number.c
More file actions
51 lines (42 loc) · 822 Bytes
/
smallest_number.c
File metadata and controls
51 lines (42 loc) · 822 Bytes
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
41
42
43
44
45
46
47
48
49
50
51
/*
Smallest Number - GeeksForGeeks
Problem Link: https://practice.geeksforgeeks.org/problems/min-sum-formed-by-digits/0
Author: Shyam Kumar
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int smallest_number( int s, int d ) {
int sm, i, lg_s;
/* Smallest d digit number */
sm = pow(10,d-1);
/* Largest sum of d digits*/
lg_s = 9 * d;
/* s is greater than largest sum */
if( s > lg_s )
return -1;
/* Decreasing 1 from total sum required */
s--;
for( i = 0; i < d; i++ ) {
if( s >= 9) {
sm = sm + 9 * pow(10,i);
s = s-9;
}
else {
sm = sm + s * pow(10,i);
break;
}
}
return sm;
}
int main(int argc, char const *argv[])
{
int t, s, d, ans;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&s, &d);
ans = smallest_number( s, d );
printf("%d\n",ans);
}
return 0;
}