fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int findFirst(int* nums, int length, int target)
  5. {
  6. if (nums == nullptr || length <= 0)
  7. return -1;
  8.  
  9. int index = 0;
  10. while (index < length && nums[index] != target)
  11. {
  12. int delta = target - nums[index];
  13. index += abs(delta);
  14. }
  15.  
  16. if (index < length)
  17. return index;
  18.  
  19. return -1;
  20. }
  21.  
  22. /************************* test code *****************************/
  23. bool isFirst(int* nums, int length, int target, int index)
  24. {
  25. for (int i = 0; i < index; ++i)
  26. {
  27. if (nums[i] == target)
  28. return false;
  29. }
  30.  
  31. if (nums[index] == target)
  32. return true;
  33.  
  34. return false;
  35. }
  36.  
  37. void test(const char* testName, int* nums, int length, int target, bool isIn)
  38. {
  39. int result = findFirst(nums, length, target);
  40. if ((isIn && result >= 0 && isFirst(nums, length, target, result))
  41. || (!isIn && result < 0))
  42. {
  43. printf("%s passed. Result is %d. \n", testName, result);
  44. }
  45. else
  46. {
  47. printf("%s FAILED.\n", testName);
  48. }
  49. }
  50.  
  51. void testGroup1()
  52. {
  53. int nums[] = { 4, 5, 6, 5, 6, 7, 8, 9, 10, 9 };
  54.  
  55. test("test1_1", nums, sizeof(nums) / sizeof(int), 0, false);
  56. test("test1_2", nums, sizeof(nums) / sizeof(int), 1, false);
  57. test("test1_3", nums, sizeof(nums) / sizeof(int), 2, false);
  58. test("test1_4", nums, sizeof(nums) / sizeof(int), 3, false);
  59. test("test1_5", nums, sizeof(nums) / sizeof(int), 4, true);
  60. test("test1_6", nums, sizeof(nums) / sizeof(int), 5, true);
  61. test("test1_7", nums, sizeof(nums) / sizeof(int), 6, true);
  62. test("test1_8", nums, sizeof(nums) / sizeof(int), 7, true);
  63. test("test1_9", nums, sizeof(nums) / sizeof(int), 8, true);
  64. test("test1_10", nums, sizeof(nums) / sizeof(int), 9, true);
  65. test("test1_11", nums, sizeof(nums) / sizeof(int), 10, true);
  66. test("test1_12", nums, sizeof(nums) / sizeof(int), 11, false);
  67. }
  68.  
  69. void testGroup2()
  70. {
  71. int nums[] = { 8, 7, 6, 7, 6, 5, 4, 3, 2, 3, 4, 3 };
  72.  
  73. test("test2_1", nums, sizeof(nums) / sizeof(int), 0, false);
  74. test("test2_2", nums, sizeof(nums) / sizeof(int), 1, false);
  75. test("test2_3", nums, sizeof(nums) / sizeof(int), 2, true);
  76. test("test2_4", nums, sizeof(nums) / sizeof(int), 3, true);
  77. test("test2_5", nums, sizeof(nums) / sizeof(int), 4, true);
  78. test("test2_6", nums, sizeof(nums) / sizeof(int), 5, true);
  79. test("test2_7", nums, sizeof(nums) / sizeof(int), 6, true);
  80. test("test2_8", nums, sizeof(nums) / sizeof(int), 7, true);
  81. test("test2_9", nums, sizeof(nums) / sizeof(int), 8, true);
  82. test("test2_10", nums, sizeof(nums) / sizeof(int), 9, false);
  83. test("test2_11", nums, sizeof(nums) / sizeof(int), 10, false);
  84. test("test2_12", nums, sizeof(nums) / sizeof(int), 11, false);
  85. }
  86.  
  87. void testGroup3()
  88. {
  89. int nums[] = { 6, 5, 4, 3, 4, 5, 6, 7, 8 };
  90.  
  91. test("test3_1", nums, sizeof(nums) / sizeof(int), 0, false);
  92. test("test3_2", nums, sizeof(nums) / sizeof(int), 1, false);
  93. test("test3_3", nums, sizeof(nums) / sizeof(int), 2, false);
  94. test("test3_4", nums, sizeof(nums) / sizeof(int), 3, true);
  95. test("test3_5", nums, sizeof(nums) / sizeof(int), 4, true);
  96. test("test3_6", nums, sizeof(nums) / sizeof(int), 5, true);
  97. test("test3_7", nums, sizeof(nums) / sizeof(int), 6, true);
  98. test("test3_8", nums, sizeof(nums) / sizeof(int), 7, true);
  99. test("test3_9", nums, sizeof(nums) / sizeof(int), 8, true);
  100. test("test3_10", nums, sizeof(nums) / sizeof(int), 9, false);
  101. test("test3_11", nums, sizeof(nums) / sizeof(int), 10, false);
  102. test("test3_12", nums, sizeof(nums) / sizeof(int), 11, false);
  103. }
  104.  
  105. int main()
  106. {
  107. testGroup1();
  108. testGroup2();
  109. testGroup3();
  110. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
test1_1 passed. Result is -1. 
test1_2 passed. Result is -1. 
test1_3 passed. Result is -1. 
test1_4 passed. Result is -1. 
test1_5 passed. Result is 0. 
test1_6 passed. Result is 1. 
test1_7 passed. Result is 2. 
test1_8 passed. Result is 5. 
test1_9 passed. Result is 6. 
test1_10 passed. Result is 7. 
test1_11 passed. Result is 8. 
test1_12 passed. Result is -1. 
test2_1 passed. Result is -1. 
test2_2 passed. Result is -1. 
test2_3 passed. Result is 8. 
test2_4 passed. Result is 7. 
test2_5 passed. Result is 6. 
test2_6 passed. Result is 5. 
test2_7 passed. Result is 2. 
test2_8 passed. Result is 1. 
test2_9 passed. Result is 0. 
test2_10 passed. Result is -1. 
test2_11 passed. Result is -1. 
test2_12 passed. Result is -1. 
test3_1 passed. Result is -1. 
test3_2 passed. Result is -1. 
test3_3 passed. Result is -1. 
test3_4 passed. Result is 3. 
test3_5 passed. Result is 2. 
test3_6 passed. Result is 1. 
test3_7 passed. Result is 0. 
test3_8 passed. Result is 7. 
test3_9 passed. Result is 8. 
test3_10 passed. Result is -1. 
test3_11 passed. Result is -1. 
test3_12 passed. Result is -1.