zoukankan      html  css  js  c++  java
  • 数组面试题

    数组面试题

    #include<iostream>
    using namespace std;
    //数组求和
    int sum(int *a, int n)
    {
    	return n == 0 ? 0 : sum(a, n - 1) + a[n - 1];
    }
    //递归法数组求和
    int sum2(int *a, int n)
    {
    	int s = 0;
    	for (int i = n - 1; i > 0; i--){
    		s += a[i];
    	}
    	return s;
    }
    //求数组的最大最小值
    void Max_Min(int *a, int n, int &max_value, int &min_value)
    {
    	max_value = a[0];
    	min_value = a[0];
    	for (int i = 0; i < n; i++){
    		if (a[i] > max_value)
    			max_value = a[i];
    		if (a[i] < min_value)
    			min_value = a[i];
    	}
    }
    //求数组中出现次数超过一半的元素
    int halftime_val(int *a, int n)
    {
    	typedef struct valuetype{
    		int num;
    		int time;
    	}value;
    	int target_val = -1;
    	value* data = new value[n];
    
    	for (int i = 0; i < n; i++) //统计数组元素出现的次数
    	{
    		data[i].num = a[i];
    		data[i].time = 1;
    		for (int j = 0; j < i; j++){
    			if (data[i].num == data[j].num)
    				data[i].time++;
    		}
    	}
    	for (int i = 0; i < n; i++){
    		if (data[i].time > n / 2){
    			target_val = data[i].num;
    		}
    	}
    	delete data;
    	return target_val;
    }
    
    // 找出数组中出现次数超过一半的元素
    int Find(int* a, int n)
    {
    	int curValue = a[0];
    	int count = 1;
    
    	for (int i = 1; i < n; ++i)
    	{
    		if (a[i] == curValue)
    			count++;
    		else
    		{
    			count--;
    			if (count < 0)
    			{
    				curValue = a[i];
    				count = 1;
    			}
    		}
    	}
    
    	return curValue;
    }
    
    //求数组中距离最小的两点
    //给定一个含有n个元素的整型数组,找出数组中的两个元素x和y使得abs(x - y)值最小
    //先对数组排序,然后遍历一次即可
    int compare(const void* a, const void* b)
    {
    	return(*(int*)a - *(int*)b);
    }
    void Min_Distance(int *a, int n)
    {
    	qsort(a, n, sizeof(int), compare);
    	int min_distace = 0;
    	int temp = 0, x1 = 0, x2 = 0;
    	min_distace = a[1] - a[0];
    	for (int i = 1; i < n - 1; i++)
    	{
    		temp = abs(a[i + 1] - a[i]);
    		if (temp < min_distace)
    		{
    			min_distace = temp;
    			x1 = a[i + 1];
    			x2 = a[i];
    		}
    	}
    	cout << min_distace << endl;
    	cout << x1 <<" "<< x2 << endl;
    }
    //求两个有序数组的共同元素
    /*
    给定两个含有n个元素的有序(非降序)整型数组a和b,求出其共同元素,比如
    a = 0, 1, 2, 3, 4
    b = 1, 3, 5, 7, 9
    输出 1, 3
    ***************************************************************
    分析:充分利用数组有序的性质,用两个指针i和j分别指向a和b,比较a[i]和b[j],根据比较结果移动指针,则有如下三种情况
    
    1. a[i] < b[j],则i增加1,继续比较
    2. a[i] == b[j],则i和j皆加1,继续比较
    3. a[i] < b[j],则j加1,继续比较
    重复以上过程直到i或j到达数组末尾。
    */
    void Find_Commom1(int *a, int *b, int n)
    {
    	int i = 0, j = 0;
    	while (i < n && j < n){
    		if (a[i] < b[j]) i++;
    		if (a[i] == b[j]){
    			cout << a[i] << " ";
    			i++;
    			j++;
    		}
    		if (a[i] > b[j]) j++;
    	}
    	cout << endl;
    }
    void Find_Commom(int* a, int *b, int n)
    {
    	int i, j;
    	for (i = 0; i < n; i++){
    		for (j = 0; j < n; j++){
    			if (a[i] == b[j])
    				cout << a[i] << " ";
    		}
    	}
    	cout << endl;
    }
    
    int main()
    {
    	int a[4] = { 2, 4, 6, 8 };
    	int b[5] = { 1, 5, 7, 9 };
    	Find_Commom(a, b, 4);
    	Find_Commom1(a, b, 4);
    }
    

      来自:http://www.cnblogs.com/graphics/archive/2010/08/24/1761620.html#

  • 相关阅读:
    AcWing 243. 一个简单的整数问题2 (树状数组)打卡
    AcWing 241. 楼兰图腾 (树状数组)打卡
    AcWing 233. 换教室 (期望DP+floyd)打卡
    AcWing 234. 放弃测试 (01分数规划)打卡
    AcWing 232. 守卫者的挑战 (期望DP)打卡
    AcWing 231. 天码 (容斥)打卡
    AcWing 230. 排列计数 水题(组合数+错排)打卡
    AcWing 229. 新NIM游戏 (线性基+博弈论)打卡
    AcWing 228. 异或 (dfs+线性基)打卡
    pstStream->pstPack[i].pu8Addr详解
  • 原文地址:https://www.cnblogs.com/mrethan/p/4190742.html
Copyright © 2011-2022 走看看