SimeonSheytanov

Sherlock and the Beast

Mar 22nd, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6.  
  7. class Solution
  8. {
  9.     static void Main(String[] args)
  10.     {
  11.         int numsCount = Convert.ToInt32(Console.ReadLine());
  12.         for(int i = 0; i < numsCount; i++)
  13.         {
  14.             int n = Convert.ToInt32(Console.ReadLine());
  15.            
  16.             if(n % 3 == 0)
  17.             {
  18.                 Console.WriteLine(new string('5', n));
  19.             }
  20.             else if(n%5 == 0)
  21.             {
  22.                 Console.WriteLine(new string('3', n));
  23.             }
  24.             else
  25.             {
  26.                 bool isFound = false;
  27.                
  28.                 for(int j = n / 3; j >= 0; j--)
  29.                 {
  30.                     if((n - (j * 3)) % 5 == 0)
  31.                     {
  32.                         Console.WriteLine("{0}{1}", new string('5', j * 3), new string('3', n - j * 3));
  33.                         //Console.WriteLine("n-j ={0}, n = {1}, j = {2}", n-j, n, j);
  34.                         isFound=true;
  35.                         break;
  36.                     }
  37.                 }
  38.                 if(!isFound)
  39.                 {
  40.                     for(int j = n / 5; j >= 0; j--)
  41.                     {
  42.                         if((n - (j * 5)) % 3 == 0)
  43.                         {
  44.                             Console.WriteLine("{0}{1}", new string('3', j * 5), new string('5', n - j * 5));
  45.                         //Console.WriteLine("n-j ={0}, n = {1}, j = {2}", n-j, n, j);
  46.                             isFound=true;
  47.                             break;
  48.                         }
  49.                     }
  50.                 }
  51.                 if(!isFound)
  52.                 {
  53.                     Console.WriteLine(-1);
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment