zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    第3次实践作业
    第2次实践作业
    第09组 团队Git现场编程实战
    第二次结对编程作业
    团队项目-需求分析报告
    团队项目-选题报告
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    课程设计第十四天
  • 原文地址:https://www.cnblogs.com/hoppz/p/14076109.html
Copyright © 2011-2022 走看看