Sherlock and the Beast

Source: Internet
Author: User

Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlock ' s Companion, Dr. Watson, suggests Moriarty May is responsible for MI6 's recent issues with their supercomputer, C1>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 are to find the largest Decent number has digits.

A Decent number has the following properties:

    1. Its digits can is only 3' s and/or 5' s.
    2. The number of 3' s It contains is divisible by 5.
    3. The number of 5' s It contains is divisible by 3.
    4. If there is 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 was an integer, denoting the number of test cases.

the subsequent lines each contain an integer, and detailing the number of digits in the number.

Output Format

Print the largest Decent number has digits; If no such number exists, tell Sherlock by printing -1.

Sample Input

413511

Sample Output

-15553333355555533333
题目大意:组成的数: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 namespacestd;7 8 9 intMain () {Ten     intT; OneCIN >>T; A      for(inta0 =0; A0 < T; a0++){ -         intN; -CIN >>N; the         intDigit_five, digit_three, flag =0; -          for(inti = n; I >=0; i--){ -             ifI3==0&& (n-i)%5==0){ -Digit_five =i; +Digit_three = N-i; -Flag =1; +                  Break; A             } at         } -         if(Flag = =0){ -cout <<"-1"<<Endl; -             Continue; -         } -          for(inti =0; i < digit_five; i++) incout <<'5'; -          for(inti =0; i < Digit_three; i++) tocout <<'3'; +cout <<Endl; -     } the     return 0; *}
View Code

Sherlock and the Beast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.