找出数组中超出总数1/4的数字
来源:互联网 发布:php大牛的博客 编辑:程序博客网 时间:2024/06/05 06:26
#include <iostream>//上一题的扩展,有3个数字出现次数超过1/4。using namespace std;void Grial(int a[], int n){ if (n <= 3)return; int count1=0, key1=0; int count2=0, key2=0; int count3=0, key3=0; for (int i = 0; i < n; i++) { if (!count1 && key2 != a[i] && key3 != a[i]) { count1++; key1 = a[i]; } else if (key1 == a[i]) { count1++; } else if (key2!=a[i] && key3!=a[i]) { count1--; } if (!count2 &&key3 != a[i] && key1!=a[i]) { count2++; key2 = a[i]; } else if (key2 == a[i]) { count2++; } else if (key1!=a[i] && key3!=a[i]) { count2--; } if (!count3 && key1!=a[i] && key2!=a[i]) { count3++; key3 = a[i]; } else if (key3 == a[i]) { count3++; } else if (key1!=a[i] && key2!=a[i]) { count3--; } } cout << key1 << endl; cout << key2 << endl; cout << key3 << endl;}int main(){ int a[] = {1,5,5,5,5,2,3,1,2,2,1,1,1,2}; Grial(a, sizeof(a) / sizeof(int)); return 0;}
0 0
- 找出数组中超出总数1/4的数字
- 快速找出数组中元素数目超出一半的元素
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中出现次数最多的数字&找出数组中只出现一次的数字
- 找出数组中最多的元素,已确定最多元素个数超过总数的一半
- 在数组中找出四个数字的和等于指定数字(4Sum)
- 面试题:如何找出数组里出现次数超过总数1/3的数
- 算法(二)找出数组中元素数目超出一半的元素
- 找出数组中两个只出现一次的数字
- 找出数组中两个只出现一次的数字--总结
- 找出数组中两个只出现一次的数字
- 找出数组中两个只出现一次的数字
- 找出数组中唯一出现一次的两个数字
- 找出数组中两个只出现一次的数字
- 找出数组中出现次数超过长度一半的数字
- Camera基本开发知识
- NO.7 HTML之继续列表之旅
- C#中的ToolStrip控件的使用
- 配置BaseApplication,方便项目中随时调用上下文Context
- python学习-装饰器
- 找出数组中超出总数1/4的数字
- 如何安装MySQL5.0图解教程
- Android6.0指纹识别开发实例
- Android相关
- Hbase 学习总结
- PAT B1049
- static,const以及extern的区别
- [JZOJ 4855]荷花池塘
- 曾国藩家训:三个地方看一个家庭的兴败