c_cpp 天花板在排序的数组中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 天花板在排序的数组中相关的知识,希望对你有一定的参考价值。

#include<iostream>
using namespace std;

int main(){
    int n,x;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    cin>>x;

    if(x<a[0])//use exit() at each line to terminate when found the answer.
        cout<< "floor doesn't exist in array,  ceil is "<<a[0];
    if (x>a[n-1])
        cout<< "floor is "<<a[n-1]<< ",  ceil doesn't exist in array";

    if (a[n-1] == x)
            cout<< "floor is " << a[n-1] << ", ceil is " << a[n-1];

    for(int i=0;i<n-1;i++){
        if (a[i] == x){
            cout<< "floor is " << a[i] << ", ceil is " << a[i];
            break;
        }
        if (a[i] < x && a[i+1] > x){
            cout<< "floor is " << a[i] << ", ceil is " << a[i+1];
            break;
        }
    }
}

以上是关于c_cpp 天花板在排序的数组中的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 找到在排序数组中出现一次的元素

c_cpp 在排序数组中,找到最接近给定数字的数字

c_cpp 通过最小增量使元素在排序数组中不同

c_cpp 非排序数组中的最小元素

c_cpp 排序数组中的绝对不同计数

c_cpp 计算已排序数组中的出现次数