fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char str[200];
  5. int nums[100], n = 0, target;
  6.  
  7.  
  8. fgets(str, sizeof(str), stdin);
  9.  
  10.  
  11. int i = 0, num = 0, reading = 0;
  12. while (str[i] != ']') {
  13. if (str[i] >= '0' && str[i] <= '9') {
  14. num = num * 10 + (str[i] - '0');
  15. reading = 1;
  16. } else {
  17. if (reading) {
  18. nums[n++] = num;
  19. num = 0;
  20. reading = 0;
  21. }
  22. }
  23. i++;
  24. }
  25. if (reading) nums[n++] = num;
  26.  
  27.  
  28. target = 0;
  29. reading = 0;
  30. while (str[i] != '\0') {
  31. if (str[i] >= '0' && str[i] <= '9') {
  32. target = target * 10 + (str[i] - '0');
  33. reading = 1;
  34. }
  35. i++;
  36. }
  37.  
  38.  
  39. int first = -1, last = -1;
  40. for (int j = 0; j < n; j++) {
  41. if (nums[j] == target) {
  42. first = j;
  43. break;
  44. }
  45. }
  46. for (int j = n - 1; j >= 0; j--) {
  47. if (nums[j] == target) {
  48. last = j;
  49. break;
  50. }
  51. }
  52.  
  53. printf("%d %d", first, last);
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5320KB
stdin
5 7
stdout
2 2