CodeChef-Enormous Input Test

using System;

public class Program
{
   public static void Main()
   {
      string lines = Console.ReadLine();
      string[] splitLines = lines.Split(' ');

      int n = int.Parse(splitLines[0]);
      int k = int.Parse(splitLines[1]);
      int[] t = new int[n];
      int output = 0;

      for(int i = 0; i < n; i++)
      {
         t[i] = int.Parse(Console.ReadLine());
      }

      foreach(int i in t)
      {
         if(i % k == 0)
         {
            output++;
         }
      }

      Console.WriteLine(output);
   }
}

I’m now officially addicted to CodeChef. These problems are so much fun. This is my solution to another beginner problem. In this one, the user inputs two integers separated by a space(hints the split with a ‘ ‘ on line 8). The first integer is ‘n’ and it indicates how many numbers are going to be tested. The second integer is ‘k’ and this is going to be used to find out if each subsequent integer is a multiple of ‘k’.

Here’s a link to the problem.

Advertisements

CodeChef-ATM

using System;

public class Test
{
   public static void Main()
   {
      string line = Console.ReadLine();
      string[] splitLines = line.Split(' ');

      double withdrawal = double.Parse(splitLines[0]);
      double balance = double.Parse(splitLines[1]);

      if(withdrawal % 5 == 0 && balance >= withdrawal + 0.5)
      {
         double updatedBalance = balance - withdrawal - 0.5;
         Console.WriteLine(updatedBalance.ToString("F"));
      }
      else
      {
         Console.WriteLine(balance.ToString("F"));
      }
   }
}

Here’s my solution to the ATM problem on CodeChef.  The user has to input a withdrawal amount and a balance amount on the same line(Why? IDK).  There is an ATM usage fee of $0.50 that must be added to the withdrawal.  The withdrawal of course cannot exceed the balance and must be a multiple of 5.  Simple may this problem be, I actually felt like I learned few things from doing it.

  1. Split() is a really nifty method for when you need to split strings.
  2. Format specifiers, in this case “F”, are really nifty for when you need to specify precision.
  3. Try not to get big-headed even when doing simple tasks.  Next thing you know, you’ll have a logical brain fart, get frustrated, and slip into an existential crisis.

Here’s the link to the problem.