Sherlock and The Beast

Sort by

recency

|

535 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can have the implementation link here : https://youtu.be/R01NQ4Zt2qA

    void decentNumber(int n) {
        int three = ((3 - (n % 3)) % 3 ) * 5;
        if(three > n) cout << -1;
        else cout << string(n - three, '5') << string(three, '3');
        cout << endl;
    }
    
  • + 0 comments
    def decentNumber(n):
        for i in range(n + 1):
            if (n - i) % 3 == 0 and i % 5 == 0:
                print((n - i)*'5' + (i)*'3')
                return
        print(-1)
    
  • + 0 comments

    python

    def decomposeNumberBy3and5(n):
        """If possible, get {3: a, 5: b}, such that n = a*3 + b*5.
        3 has the priority."""
        if n in (1, 2, 4, 7):
            return None
    
        match n % 3:
            case 0:
                return {3: n // 3, 5: 0}
            case 1:
                return {3: n // 3 - 3, 5: 2}  # 1 = 5 * 2 - 3 * 3
            case 2:
                return {3: n // 3 - 1, 5: 1}  # 2 = 5 * 1 - 3 * 1
    
    
    def decentNumber(n):
        if (decomp := decomposeNumberBy3and5(n)) is None:
            print(-1)
        else:
            print("5" * decomp[3]*3 + "3" * decomp[5]*5)
    
  • + 0 comments
    public static void decentNumber(int n) {
            String result = "-1";
            
            if(n%3 == 0) {
                result = "5".repeat(n);
            } else {
                for(int i = 5; i <= n; i+=5) {
                    if((n-i)%3 == 0) {
                        result = "5".repeat(n-i) + "3".repeat(i);
                        break;
                    }
                }
            }
          System.out.println(result);
        }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM_)
    
    solve :: String -> String
    solve nstr = do
        let n = read nstr :: Int
        let res = n `divMod` 3
        case go res of
            Nothing -> "-1"
            Just (q, r) -> replicate (q * 3) '5' ++ replicate r '3'
      where
        go :: (Int, Int) -> Maybe (Int, Int)
        go (q, r)
            | q < 0 = Nothing
            | r `mod` 5 == 0 = Just (q, r)
            | otherwise = go (q - 1, r + 3)
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            n <- getLine
            putStrLn $ solve n