Advertisement
Guest User

Untitled

a guest
Jan 10th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class examSubSetSums
  5. {
  6. static void Main(string[] args)
  7. {
  8. long s = long.Parse(Console.ReadLine());
  9. byte n = byte.Parse(Console.ReadLine());
  10. byte k = byte.Parse(Console.ReadLine());
  11. int count = 0;
  12. long[] array = new long[n];
  13. for (int i = 0; i < n; i++)
  14. {
  15. array[i] = long.Parse(Console.ReadLine());
  16. }
  17.  
  18. for (int i = 1; i < Math.Pow(2, n); i++)
  19. {
  20. char[] binaryCharArray = Convert.ToString(i, 2).PadLeft(n).ToCharArray();
  21. long subsetSum = 0;
  22. count = 0;
  23. for (int j = 0; j < binaryCharArray.Length; j++)
  24. {
  25. if (binaryCharArray[j] == '1')
  26. {
  27. subsetSum += array[j];
  28. count++;
  29. }
  30. }
  31. if (subsetSum == s&&count==k)
  32. {
  33. for (int j = 0; j < binaryCharArray.Length; j++)
  34. {
  35. if (binaryCharArray[j] == '1')
  36. {
  37. Console.Write(array[j] + " ");
  38. }
  39. }
  40. Console.Write("= {0} There is subset even to number \"s\" formed by \"k\" elements.", s);
  41. Console.WriteLine();
  42. }
  43.  
  44. }
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement