2014-05-07 15:17

题目链接

原题:

Given an array of n elements (a1,a2,..ai,...,an). You are allow to chose any index i and j, such that (i!=j) and allow to perform increment operation on ai = ai+ and decrements operation on aj = aj -  infinite number of times. How many maximum number of elements you can find that have same number.

题目:给定一个长度为n的整数数组。每次允许你选取其中两个元素,对一个+1,对另一个-1。如果允许你执行任意多次这样的操作,问你至多能把多少个元素变成同一个数?

解法:第一次看这个题目还看不到懂,但想通了以后发现就是解题几乎就是一句话的事情。每次进行这样的操作时,所有元素加起来的总和一直保持不变,所以只要看看总和能否被n整除。如果能整除,则n个数都会变成一样。否则,只能得到n - 1个一样的数,剩下一个不一样。最后,注意取模运算对于负数的处理。

代码:

 // http://www.careercup.com/question?id=6407924087783424
#include <iostream>
#include <vector>
using namespace std; inline int mod(int x, int y)
{
return x % y >= ? x % y : y - (y - x) % y;
} int main()
{
vector<int> v;
int n;
int i;
int sum; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
} sum = ;
for (i = ; i < n; ++i) {
sum = mod(sum + v[i], n);
}
cout << (sum ? n - : n) << endl;
v.clear();
} return ;
}

Careercup - Google面试题 - 6407924087783424的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. POJ 1195

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13774   Accepted: 6393 De ...

  2. LLVM language 参考手册(译)(1)

    LLVM Language Reference Manual 摘要 这个文档是一个LLVM汇编语言的参考手册.LLVM是一个基于Static Single Assignment(SSA - 静态单赋值 ...

  3. gdb调试工具使用方法分享

    刚才看了一个CSDN上分享gdb调试工具使用的教程,讲得非常好,推荐到这里: http://blog.csdn.net/liigo/article/details/582231

  4. 如何添加localizable.strings本地化

    1.在Supporting Files文件夹右键,NewFile… -> iOS -> Resources -> String Files,命名为Localizable.string ...

  5. 软件工程 speedsnail 冲刺1

    2015-5-5 完成任务:背景音乐的添加: 遇到问题:循环播放的设置: 明日任务:snail的移动功能.

  6. objective-C基本知识

    预编译指令 1.自己写的头文件最好用双引号,而系统自带的可以用尖括号. ************************************************* 点语法 p.age= 10; ...

  7. JMeter2.13进行压力测试

    1.安装 2. 样本数目 总共发送到服务器的请求数. 最新样本 代表时间的数字,是服务器响应最后一个请求的时间 吞吐量 是服务器每分钟处理的请求数. 平均值 是总运行时间除以发送到服务器的请求数. 中 ...

  8. jQuery编写的一款兼容IE6的图片轮播幻灯片

    jQuery编写的一款兼容IE6的图片轮播幻灯片,很不错的一款jquery特效.大家可以下载下来研究研究. 每隔几秒就自动切换一波图片,此效果兼容性还做的不错,适合居多的浏览器. 适用浏览器:IE6. ...

  9. SBM is Not Sale And Run Company

    data = """ Well,We will bet you dollars to donuts,there are so many crusher provider ...

  10. js给php传值

    //ajax传值 var str= JSON.stringify(arr1);//数组转string //alert(typeof(str)); $.ajax({ url:'test.php' ,ty ...