#include <iostream>
using namespace std;
//求x!中k因数的个数。
int Grial(int x,int k)
{
int Ret = 0;
while (x)
{
Ret += x / k;
x /= k;
}
return Ret;
}
int main()
{
cout << Grial(10, 2) << endl;
return 0;
} //假设要求一个n!中k的因子个数,那么必然满足例如以下的规则。
//即x=n/k+n/k^2+n/k^3...(直到n/k^x小于0);
#include <iostream>
using namespace std;
int Grial(int x, int k)
{
int count = 0;
int n = x;
while (n)
{
n &= (n - 1);
count++;
}
return x - count;
}
int main()
{
cout << Grial(3, 2) << endl;
return 0;
} //找出数组中出现次数超过数组一半的数字。
#include <iostream>
using namespace std;
int Grial(int a[], int n)
{
int count=0;
int key;
for (int i = 0; i < n; i++)
{
if (count == 0)
{
key = a[i];
count = 1;
}
else
{
if (key == a[i])
{
count++;
}
else
{
count--;
}
}
}
return key;
}
int main()
{
int a[] = {1,2,3,4,5,6,3,3,3,3,3};
cout<<Grial(a, sizeof(a) / sizeof(int))<<endl;
return 0;
} #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;
}

C++求解数组中出现超1/4的三个数字。的更多相关文章

  1. 给定一个只包含正整数的非空数组,返回该数组中重复次数最多的前N个数字 ,返回的结果按重复次数从多到少降序排列(N不存在取值非法的情况)

    """ #给定一个只包含正整数的非空数组,返回该数组中重复次数最多的前N个数字 #返回的结果按重复次数从多到少降序排列(N不存在取值非法的情况) 解题思路: 1.设定一个 ...

  2. 《剑指offer》第五十六题(数组中只出现一次的两个数字)

    // 面试题56(一):数组中只出现一次的两个数字 // 题目:一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序 // 找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度 ...

  3. 获取数组中多个相加等于0的一组数字 javascript

    //获取数组中两个相加等于0的一对数字,比如[ [ -10, 10 ], [ -5, 5 ] ] var arr=[-5,10,1,-10,3,4,5,9] //对数组进行排序 arr.sort(fu ...

  4. 【Java】 剑指offer(56-1) 数组中只出现一次的两个数字

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程 ...

  5. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  6. 34.数组中2个只出现一次的数字[Find two numbers which appear once]

    [题目] 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). [分析] 这是一道很新颖的关于位运算的面试题. ...

  7. 【Offer】[56-1] 【数组中只出现一次的两个数字】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 一个整型数组里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是0(1). ...

  8. [LeetCode] 421. Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  9. JS判断一个数组中是否有重复值的三种方法

    方法一: var s = ary.join(",")+","; for(var i=0;i<ary.length;i++) { if(s.replace( ...

随机推荐

  1. VS2008 ActiveX(ocx控件)的调试工具ActiveX Control Test Container安装说明

    vs2008中的TSTCON( ActiveX Control Test Container )工具非自动安装,而是作为一个例程提供.所以应找到该例程,并编译: 如vs2008安装在默认路径则 1, ...

  2. 【计算机网络】2.6 P2P应用

    第二章第六节 P2P应用 在本节内容开始前,我们要先来对P2P架构有一个宏观的认知: P2P:(Peer to Peer 对等结构)   以对等方式进行通信,并不区分客户端和服务端,而是平等关系进行通 ...

  3. 19Web服务

    Web服务 Web服务 Micosoft.Net平台架构中的分布式系统主要包括两部分:用ASP.Net技术构建服务器端动态网页,以及Web服务(Web Service或XML Web Service) ...

  4. html嵌入pdf && html嵌入多媒体文件,word,flash,pdf,音视频

    <object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="1000" hei ...

  5. PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (四) -- Push API 和 Pull API

    RabbitMQ 中针对消息的分发提供了 Push API (订阅模式) 和 Pull API (主动获取) 两种模式. 在 PHP 中, 这两种模式分别通过 AMQPQueue 类中的 consum ...

  6. 第2节 mapreduce深入学习:7、MapReduce的规约过程combiner

    第2节 mapreduce深入学习:7.MapReduce的规约过程combiner 每一个 map 都可能会产生大量的本地输出,Combiner 的作用就是对 map 端的输出先做一次合并,以减少在 ...

  7. div 可视化区域弹窗居中

    效果: css: .div_alt { position: fixed; border-radius: 5px; top: 50%; left: 50%; width: auto; min-width ...

  8. jsMap地图网点

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...

  9. js 右键菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Samba 学习笔记

    这个网站不错.https://www.ibm.com/developerworks/cn/linux/l-lpic3-311-1/