Advertisement
sylviapsh

Guitar Volumes

Feb 2nd, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2.  
  3. class Guitar
  4. {
  5.   static void Main()
  6.   {
  7.     string volumeChangesInput = Console.ReadLine();
  8.     int initialVolB = int.Parse(Console.ReadLine()),
  9.         maxVolM = int.Parse(Console.ReadLine());
  10.     string[] delimiters = { ", " };
  11.     string [] volumeChanges = volumeChangesInput.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
  12.  
  13.     int[,] results = new int[volumeChanges.Length + 1, maxVolM+1];
  14.  
  15.     //Fill in the first row the initial volume level
  16.     results[0, initialVolB] = 1;
  17.      
  18.     //Fill the table with the possible changes
  19.     for (int row = 1; row < results.GetLength(0); row++)
  20.     {
  21.       for (int col = 0; col < results.GetLength(1); col++)
  22.       {
  23.         if (results[row-1,col] == 1)
  24.         {
  25.           int tempUp =  col+ int.Parse(volumeChanges[row-1]);
  26.           int tempDown = col - int.Parse(volumeChanges[row-1]);
  27.  
  28.           if (tempUp >= 0 && tempUp <= maxVolM)//possible volume
  29.           {
  30.             results[row, tempUp] = 1;
  31.           }
  32.           if (tempDown >= 0 && tempDown <= maxVolM)
  33.           {
  34.             results[row, tempDown] = 1;
  35.           }
  36.         }
  37.       }
  38.     }
  39.  
  40.     //Find the result and print it
  41.     int maxVol = -1;
  42.     for (int i = results.GetLength(1)-1; i >= 0; i--)
  43.     {
  44.       if (results[volumeChanges.Length, i] == 1)
  45.       {
  46.         maxVol = i;
  47.         break;
  48.       }
  49.     }
  50.  
  51.     Console.WriteLine(maxVol);
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement