/
chefluck.cpp
94 lines (74 loc) · 2.11 KB
/
chefluck.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Every great chef knows that lucky numbers are positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Our chef has recently returned from the Lucky country. He observed that every restaurant in the Lucky country had a lucky number as its name.
He believes that having a lucky number as a restaurant name can indeed turn out to be very lucky.
Our chef believes that it is possible to make a lucky number having N digits even luckier. Any number following the rules below is called Lucky lucky number -
1. The number contains only digits 4 and 7.
2. Count of digit 4 in the number should be divisible by 7.
3. Count of digit 7 in the number should be divisible by 4.
Help our chef to compute the count of digit 4 in the smallest Lucky lucky number having N digits.
Input
First line contains T, number of test cases. Each of the next T lines contains a number N, the number of digits in the Lucky lucky number to be formed.
1<=T<=1000
1<=N<=1000000000 (10^9)
Output
If it is not possible to form a Lucky lucky number having N digits, output -1.
Otherwise, output the count of digit 4 in the smallest Lucky lucky number having N digits.
Example
Input:
5
7
4
11
1
15
Output:
7
0
7
-1
7
Explanation
For the last test case, N = 15, the smallest lucky lucky number is
444444477777777. The count of digit 4 is 7.
*/
#include <stdio.h>
int main()
{
int cases,N,sub,i,mul;
bool found;
scanf("%d",&cases);
while(cases--)
{
scanf("%d",&N);
if(N<7)
{
if(N==4)
{
printf("%d\n",0);
continue;
}else
{
printf("%d\n",-1);
continue;
}
}
i=N/7;
while(i>0)
{
found=false;
mul=7*i;
sub=N-mul;
if(sub%4==0)
{
found=true;
break;
}
i=i-1;
}
if(found)
printf("%d\n",mul);
else
printf("%d\n",-1);
}
}