sylviapsh

Winning Tickets

Dec 27th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using System;
  2. class Tickets2
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //The winning tickets with 5 numbers of the lottery are those that have their middle numbers divided without remainder to their first and last number. The amount is the result of their division.
  8.     //Ticket 11082. Middle number is: 108; first and last number are: 12. 108 / 12 = 9 so this is a winning ticket with amount to be paid 9.
  9.     //The program should calculate the sum of the amounts to be paid and the number of the winning tickets.
  10.     //The input will consist of a number of one ticket on each line. The last line will be 0 and will not be a ticket number.
  11.  
  12.     int winingTickets = 0,
  13.         winningSum = 0;
  14.  
  15.     while (true)
  16.     {
  17.       string input = Console.ReadLine();
  18.       if (input == "0")
  19.       {
  20.         break;
  21.       }
  22.       else
  23.       {
  24.         char[] numbers = new char[input.Length];
  25.         int i = 0;
  26.         foreach (char item in input)
  27.         {
  28.           numbers[i] = item;
  29.           i++;
  30.         }
  31.  
  32.         string firstLast = Convert.ToString(numbers[0]) + numbers[input.Length - 1];
  33.  
  34.         string middleNum = Convert.ToString(numbers[1]);
  35.         for (int j = 2; j < input.Length - 1; j++)
  36.         {
  37.           middleNum = middleNum + Convert.ToString(numbers[j]);
  38.         }
  39.  
  40.         int result = Convert.ToInt32(middleNum) % Convert.ToInt32(firstLast);
  41.         int amount = Convert.ToInt32(middleNum) / Convert.ToInt32(firstLast);
  42.         if (result == 0)
  43.         {
  44.           winingTickets++;
  45.           winningSum += amount;
  46.         }
  47.       }
  48.     }
  49.  
  50.     Console.WriteLine(winningSum);
  51.     Console.WriteLine(winingTickets);
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment