Sherlock and The Beast
Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlock's companion, Dr. Watson, suggests Moriarty may be responsible for MI6's recent issues with their supercomputer, The Beast.
Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting The Beast with a virus; however, he also gives him a clue—a number, . Sherlock determines the key to removing the virus is to find the largest Decent Number having digits.
A Decent Number has the following properties:
- Its digits can only be 3's and/or 5's.
- The number of 3's it contains is divisible by 5.
- The number of 5's it contains is divisible by 3.
- If there are more than one such number, we pick the largest one.
Moriarty's virus shows a clock counting down to The Beast's destruction, and time is running out fast. Your task is to help Sherlock find the key before The Beast is destroyed!
Constraints
1 =< T <= 20
1 <= N <= 100000
Input Format
The first line is an integer, , denoting the number of test cases.
The subsequent lines each contain an integer, , detailing the number of digits in the number.
Output Format
Print the largest Decent Number having digits; if no such number exists, tell Sherlock by printing -1.
Sample Input
4
1
3
5
11
Sample Output
-1
555
33333
55555533333
题目大意:组成的数:5的个数能被3整除,3的个数能被5整除,找最大的那个数,显然满足条件的数中所有的5在位的前面是最大的数。5和3的个数等于n,找满足条件的5的个数最多的那个数,
一开始设5的个数为n,判断是否满足条件,否则5的个数为n-1,一直遍历,找到满足的一组则立即结束遍历,若遍历到5的个数为0,也没有找到满足条件的一组数,则输出“-1”
1 #include <cmath> 2 #include <cstdio> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 9 int main(){ 10 int t; 11 cin >> t; 12 for(int a0 = 0; a0 < t; a0++){ 13 int n; 14 cin >> n; 15 int digit_five, digit_three, flag = 0; 16 for(int i = n; i >= 0; i--){ 17 if(i % 3 == 0 && (n-i) % 5 == 0){ 18 digit_five = i; 19 digit_three = n - i; 20 flag = 1; 21 break; 22 } 23 } 24 if(flag == 0){ 25 cout << "-1" << endl; 26 continue; 27 } 28 for(int i = 0; i < digit_five; i++) 29 cout << '5'; 30 for(int i = 0; i < digit_three; i++) 31 cout << '3'; 32 cout << endl; 33 } 34 return 0; 35 }