Programming Question # # #: Looking for subscript
Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )
Note: Total time limit: 1000ms memory limit: 65536kB
Description
An array of integers is known to x[], where the elements are different from each other. Find out if there is an element in the given array that satisfies the x[i]=i relationship, and the array subscript starts at 0.
For example, if x[]={-2,-1,7,3,0,8}, then x[3] = 3, so 3 is the answer.
Input
The first line contains an integer n (0 < n < 100) that represents the number of elements in the array.
The second row contains n integers, which in turn represent the elements in the array.
Output
The output is an integer that satisfies the x[i]=i element, and if more than one element satisfies, the first satisfied element is output
If no element is satisfied, the output is "N".
Sample Input
6-2-1 7 3 4 8
Sample Output
3
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 intN;7 BOOLIscout =false;8CIN >>N;9 intA[n];Ten for(inti =0; I < n; i++) { OneCIN >>A[i]; A } - for(inti =0; I < n; i++) { - if(A[i] = =i) { thecout <<A[i]; -Iscout =true; - Break; - } + } - if(!iscout) { +cout <<"N"; A } at return 0; -}
POJ C Programming Advanced Programming Question # #: Looking for subscript