We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Greedy
  4. Sherlock and The Beast
  5. Discussions

Sherlock and The Beast

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

    You are viewing a single comment's thread. Return to all comments →

  • SergioGM 5 years ago+ 0 comments

    Hey man, I didnt know how StringBuilder worked, but still got the solution reducing the number of iterations.

    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
    
        for (int i=0;i<t;i++){
    
            int n = in.nextInt();
            String sol3 = "", sol5 = "";
            String staux = "555";
    
            while(n>0){
                if (n%3==0)
                    break;                                    
                else{                    
                    sol3+="33333";
                    n-=5;                    
                }
            }            
    
            while (n>0){
    
                if (staux.length()<=n){
                    sol5+=staux;
                    n-=staux.length();
                    staux+=staux;
                }
                else
                    if (n<3) 
                        break;
                    else
                        staux="555";
    
            }
    
            if (n==0)
                System.out.println(sol5+sol3);
            else
                System.out.println(-1);
    
        } 
    }
    

    Still much better using Stringbuilder, but not impossible with strings ;) Thanks for the info.

    1|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature