-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWinning Lottery-Greedy Problem Coding Ninjas CP Course
53 lines (51 loc) · 1.37 KB
/
Winning Lottery-Greedy Problem Coding Ninjas CP Course
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
52
53
Winning Lottery
Send Feedback
Harshit knows through his resources that this time the winning lottery number is the smallest number whose sum of the digits is S and the number of digits is D. You have to help Harshit and print the winning lottery number.
Input Format:
First line of input contians an integer T, representing the number of test cases.
Next T lines contains two space-separated integers: S,D
Constraints:
1 <= T <= 1000
1 <= D <= 1000
1 <= S <= 9*D
Time Limit: 1 second
Output Format
The output contains a single integer denoting the winning lottery number
Sample Input 1:
1
9 2
Sample Output 1:
18
Explanation
There are many other possible numbers like 45, 54, 90, etc with the sum of digits as 9 and number of digits as 2. The smallest of them is 18.
#include<bits/stdc++.h>
using namespace std;
int main(){
// write your code here
int t;cin>>t;
while(t--){
int s,d;cin>>s>>d;
int arr[d]={0};
arr[0]=1;
int temp=d;
s-=1;
while(s>0){
while(temp>=0){
if(s>=9){
arr[temp-1]+=9;
s-=9;
}
else{
arr[temp-1]+=s;
s=0;
}
temp--;
}
}
for(int i=0;i<d;i++){
cout<<arr[i];
}
cout<<endl;
}
return 0;
}