Advertisement
Guest User

HW (Flagpole)

a guest
Dec 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace RecursiveOperatorsCombinations
  4. {
  5. public class Program
  6. {
  7. public static void Main()
  8. {
  9. int intLength = int.Parse(Console.ReadLine());
  10. int targetValue = int.Parse(Console.ReadLine());
  11.  
  12. if (PrintValidSolutions(intLength, targetValue, " = " + targetValue))
  13. {
  14. return;
  15. }
  16.  
  17. Console.WriteLine("N/A");
  18. }
  19.  
  20. private static bool PrintValidSolutions(int intLength, int targetValue, string currentString)
  21. {
  22. if (intLength == 1)
  23. {
  24. if (targetValue == 1)
  25. {
  26. Console.Write("1" + currentString + "\n");
  27. return true;
  28. }
  29.  
  30. return false;
  31. }
  32.  
  33. bool found = PrintValidSolutions(intLength - 1, targetValue + intLength, " - " + intLength + currentString);
  34.  
  35. if (PrintValidSolutions(intLength - 1, targetValue - intLength, " + " + intLength + currentString))
  36. {
  37. found = true;
  38. }
  39.  
  40. return found;
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement