Advertisement
svetoslavhl

Untitled

May 21st, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. //6. Write a program that reads two integer numbers N and K and an array of N elements from the console.
  2. //Find in the array those K elements that have maximal sum.
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7.  
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("Enter the N value :");
  13. int N = int.Parse(Console.ReadLine());
  14.  
  15. Console.WriteLine("Enter the K value :");
  16. int K = int.Parse(Console.ReadLine());
  17.  
  18. int[] arr = new int[N];
  19.  
  20. //Entering the values of each item in the array
  21.  
  22. for (int i = 0; i < N; i++)
  23. {
  24.  
  25. Console.Write("array[{0}] = ", i);
  26. arr[i] = int.Parse(Console.ReadLine());
  27.  
  28.  
  29. }
  30.  
  31. //Оrdering the values in the array descendingly using the bubble method
  32.  
  33. int aux = 0;
  34.  
  35. for (int a = 0; a < N; a++)
  36. {
  37.  
  38.  
  39.  
  40. for (int b = 0; b < N - 1; b++)
  41. {
  42.  
  43. if (arr[b] < arr[b + 1])
  44. {
  45. aux = arr[b];
  46. arr[b] = arr[b + 1];
  47. arr[b + 1] = aux;
  48.  
  49.  
  50. }
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. Console.Write("The {0} elements that have maximal sum in the array are: ", K);
  72. for (int i = 0; i < K; i++)
  73. {
  74.  
  75. Console.Write("{0}", arr[i]);
  76. if (i != K - 1)
  77. {
  78. Console.Write(", ");
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. }
  86.  
  87.  
  88.  
  89. Console.WriteLine();
  90.  
  91.  
  92.  
  93.  
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement