POJ C Programming Advanced Programming Question # #: Looking for subscript

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.