Sherlock and The Beast code in C#

Problem source:

Sherlock Holmes is getting paranoid about Professor Moriarty, his arch-enemy. All his efforts to subdue Moriarty have been in vain. These days Sherlock is working on a problem with Dr. Watson. Watson mentioned that the CIA has been facing weird problems with their supercomputer, ‘The Beast’, recently.

This afternoon, Sherlock received a note from Moriarty, saying that he has infected ‘The Beast’ with a virus. Moreover, the note had the number N printed on it. After doing some calculations, Sherlock figured out that the key to remove the virus is the largest Decent Number having N digits.

A Decent Number has the following properties:

  1. 3, 5, or both as its digits. No other digit is allowed.
  2. Number of times 3 appears is divisible by 5.
  3. Number of times 5 appears is divisible by 3.

Meanwhile, the counter to the destruction of ‘The Beast’ is running very fast. Can you save ‘The Beast’, and find the key before Sherlock?

Input Format
The 1st line will contain an integer T, the number of test cases. This is followed by T lines, each containing an integer N. i.e. the number of digits in the number.

Output Format
Largest Decent Number having N digits. If no such number exists, tell Sherlock that he is wrong and print 1.


Sample Input


Sample Output





class Sherlock_And_The_Beast
static void Main(string[] args)
int T = Convert.ToUInt16(Console.ReadLine());
for (int i = 0; i < T; ++i)
int N = Convert.ToInt32(Console.ReadLine());
int C = 5 * (2 * N % 3);

if (C > N)
Console.Write(repeat(“5”, (N – C)) + repeat(“3”, C));

public static String repeat(String str, int times)
StringBuilder ret = new StringBuilder();
for (int i = 0; i < times; i++)
return ret.ToString();



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s