• 2020_12_2_数据结构


    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 1e7;
    int a[maxn];
    int t[maxn];
    
    void sort(int n)
    {
    	int maxnum = 0;
    	for(int i = 0; i < n; i++){
    		t[a[i]]++;
    		if( a[i] > maxnum ) maxnum = a[i];
    	}
    
    	int cnt = 0;
    	for(int i = 0; i <= maxnum; i++){
    
    		if(t[i] != 0)
    		for(int j =0 ; j < t[i]; j++)
    			a[cnt++] = i;
    	}
    
    	for(int i = 0 ;i < n ; i++){
    		cout << a[i] << " ";
    	}
    	cout << endl;
    }
    
    int lower_Find(int n , int x)
    {
        int l = 0 , r = n;
        while(l < r)
        {
            int mid = (l + r) >> 1;
            if(a[mid] < x) l = mid + 1;
            else r = mid;
        }
    
        return r;
    }
    
    int upper_Find(int n ,int x)
    {
        int l = 0 ,r = n;
        while(l < r)
        {
            int mid = (l + r + 1) >> 1;
            if( a[mid] <= x ) l = mid ;
            else r = mid - 1;
        }
        return r;
    }
    
    int main()
    {
    	int n;
        cout << "请输入需要创建的数组大小: " << endl;
    	cin >> n;
    
    	memset(t,0,sizeof t);
    
    	srand( (unsigned int) time(NULL) );
    	for(int i = 0; i < n ; i++){
    		a[i] = rand()%10;
    	}
    
    	sort(n);
    
        int x;
        cout << "请输入要查找的数据" << endl;
        cin >> x;
        cout << "如果是找这个数第一次出现的位置输入1 " <<endl;
        cout << "如果是找这个数最后一次出现的位置输入2 " <<endl;
        int fx,cmd;
        cin >> cmd
        if(cmd  == 1)
            fx = lower_Find(n,x);
        else if (cmd == 2)
            fx = upper_Find(n,x);
    
        if( a[fx] == x) cout << "找到 " << x << " 在第 " << fx+1 << " 位"<< endl;
        else cout << "没有找到这个数" << endl;
    	return 0;
    }
    
    
  • 相关阅读:
    WPF编程学习——样式
    WPF编程学习——布局
    AngularJs学习笔记--concepts(概念)
    AngularJs学习笔记--html compiler
    AngularJs学习笔记--bootstrap
    Linq教程
    SQL通用分页存储过程
    JS时间倒计时
    canvas时钟可随着画布变大而比例变大
    ie下placeholder解决办法
  • 原文地址:https://www.cnblogs.com/hoppz/p/14076109.html
Copyright © 2020-2023  润新知