Advertisement
TodorovP

Crossing sequences

May 13th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _01_Crossing_sequences
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var tribFirst = int.Parse(Console.ReadLine()); //[1...1000000]
  14.             var tribSecond = int.Parse(Console.ReadLine()); //[1...1000000]
  15.             var tribThird = int.Parse(Console.ReadLine()); //[1...1000000]
  16.             var num = int.Parse(Console.ReadLine()); //[1...1000000]
  17.             var spiralStep = int.Parse(Console.ReadLine()); //[1...1000000]
  18.  
  19.             int tribCurrent = tribFirst + tribSecond + tribThird;
  20.             int spiralCurrent = num;
  21.             int spiralStepMult = 1;
  22.             int step = 1;
  23.  
  24.             while (tribCurrent <= 1000000 && spiralCurrent <= 1000000)
  25.             {
  26.                 if (tribCurrent == spiralCurrent || spiralCurrent == tribFirst
  27.                     || spiralCurrent == tribSecond || spiralCurrent == tribThird)
  28.                 {
  29.                     Console.WriteLine(spiralCurrent);
  30.                     break;
  31.                 }
  32.                 else if (tribCurrent < spiralCurrent)
  33.                 {
  34.                     tribCurrent = tribFirst + tribSecond + tribThird;
  35.                     tribFirst = tribSecond;
  36.                     tribSecond = tribThird;
  37.                     tribThird = tribCurrent;
  38.                 }
  39.                 else
  40.                 {
  41.                     spiralCurrent += spiralStepMult * spiralStep;
  42.                     if (step % 2 == 0) spiralStepMult++;
  43.                     step++;
  44.                 }
  45.             }
  46.  
  47.             if (tribCurrent > 1000000 || spiralCurrent > 1000000) Console.WriteLine("No");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement