Advertisement
aspire12

02.fromLeftToTheRight

Feb 2nd, 2020
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace _2.fromLeftToTheRight
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             /*You will receive number which represent how many lines we will get as an input. On the next N lines, you will receive a string with 2 numbers separated by single space. You need to compare them. If the left number is greater than the right number, you need to print the sum of all digits in the left number, otherwise print the sum of all digits in the right number.*/
  11.  
  12.             int lines = int.Parse(Console.ReadLine());
  13.             for (int i = 0; i < lines; i++)
  14.             {
  15.                 string input = Console.ReadLine();
  16.                 StringBuilder left = new StringBuilder();
  17.                 StringBuilder right = new StringBuilder();
  18.                 bool space = false;
  19.                 for (int j = 0; j < input.Length; j++)
  20.                 {
  21.                     if (char.Parse(Convert.ToString(input[j])) == ' ')
  22.                     {
  23.                         space = true;
  24.                         continue;
  25.                     }
  26.                     if (!space)
  27.                     {
  28.                         left.Append(input[j]);
  29.                     }
  30.                     else
  31.                     {
  32.                         right.Append(input[j]);
  33.                     }
  34.                 }
  35.                 float sum = 0;
  36.                 if (double.Parse(Convert.ToString(left)) > double.Parse(Convert.ToString(right)))
  37.                 {
  38.                    
  39.                     for (int k = 0; k < Convert.ToString(left).Length; k++)
  40.                     {
  41.                         bool symbol = int.TryParse(Convert.ToString(left[k]), out int digit);
  42.                         sum += digit;
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     for (int k = 0; k < Convert.ToString(right).Length; k++)
  48.                     {
  49.                         bool symbol = int.TryParse(Convert.ToString(right[k]), out int digit);
  50.                         sum += digit;
  51.                     }
  52.                 }
  53.                 Console.WriteLine(sum);
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement