Advertisement
andreykata

Problem08 - Needles

Oct 11th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 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 Problem08Needles
  8. {
  9. public class Needles
  10. {
  11. static List<int> result = new List<int>();
  12.  
  13. public static void Main(string[] args)
  14. {
  15. string[] firstLine = Console.ReadLine().Split(' ');
  16. int c = int.Parse(firstLine[0]);
  17. int n = int.Parse(firstLine[1]);
  18.  
  19. string[] secondLine = Console.ReadLine().Split(' ');
  20. int[] arrayC = Array.ConvertAll(secondLine, s => int.Parse(s));
  21.  
  22. string[] thirdLine = Console.ReadLine().Split(' ');
  23. int[] arrayN = Array.ConvertAll(thirdLine, s => int.Parse(s));
  24.  
  25. foreach (int number in arrayN)
  26. {
  27. for (int i = 0; i < arrayC.Length; i++)
  28. {
  29. // First case if current element is not 0
  30. if (arrayC[i] != 0)
  31. {
  32. if (number <= arrayC[i])
  33. {
  34. result.Add(i);
  35. break;
  36. }
  37.  
  38. if (i == arrayC.Length - 1)
  39. {
  40. result.Add(i + 1);
  41. break;
  42. }
  43. }
  44.  
  45. // Second case if current element is 0
  46. else
  47. {
  48. int j = i + 1;
  49. int temp = i;
  50.  
  51. // Search value diferent from 0
  52. while (j < arrayC.Length && arrayC[j] == 0)
  53. {
  54. j++;
  55. }
  56.  
  57. // Jump elements with value 0
  58. i = j - 1;
  59.  
  60. // If element with index j is not bigger then Number, we have result
  61. if (j >= arrayC.Length || number <= arrayC[j])
  62. {
  63. result.Add(temp);
  64. break;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. Console.WriteLine(string.Join<int>(" ", result));
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement