Define an array of integers and enter an integer x, if x is not in this array, returns the position of the maximum number less than x and the position of the minimum number greater than X j

Source: Internet
Author: User

Define an array of integers and enter an integer x, if x is not in this array, returns the position of the maximum number less than x and the position of the smallest number greater than X J;

If x is in this array, the position of the number in the array is returned.

Source:

#include <iostream>using namespace Std;void main () {int array[]={1,2,3,4,5,6,7,89,45,32,56,78,12,43,90,19};// 16 digits int X;int max=array[0];int min=array[0];int maxweizhi=0;int minweizhi=0;int weizhi=0;char end;cout<< " Please enter the value of positive integer x: "; cin>>x;for (int i=0;i<16;i++) {if (X==array[i]) {weizhi=i+1;break;} Else{if (array[i]>x) {if (max<=x| | Max>array[i])//The position of the smallest integer greater than x {maxweizhi=i+1;max=array[i];}} if (array[i]<x) {if (min>=x| | Min<array[i])//For the position of the largest integer less than x {minweizhi=i+1;min=array[i];}}} cout<< "array element is:"; for (int i=0;i<16;i++) {cout<<array[i]<< "  ";} Cout<<endl;if (weizhi!=0) {cout<<x<< "in the array" <<weizhi<< "position" <<ENDL;} else{cout<<x<< "not in array" <<endl;cout<< "less than" <<x<< "the maximum number of locations is" <<minweizhi< <endl;cout<< the position of the minimum number greater than <<x<< is "<<maxweizhi<<endl"; cout<< "Please press any character enter end:"; cin>>end;}


Define an array of integers and enter an integer x, if x is not in this array, returns the position of the maximum number less than x and the position of the minimum number greater than X j

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.