Careercup - Google面试题 - 6407924087783424
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的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- WP8 调用特定API权限不足
1.在解决方案中依次打开 Properties -> WMAppManifest.xml 2.点击功能 3.在左侧功能列表中勾选想要的功能权限.完毕
- js 定位到指定位置
<script> //滚动定位到product function scroll() { var scroll_offset = $(&quo ...
- Linux下CPU占用率高分析方法
一般解决方法是通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用1. 通过top命令找到可疑进程PID top - 09:37:18 up 70 days, 16:29 ...
- DataGridView取消默认选中行
DataGridView在添加数据后会默认选中第 一个单元格或者第一行,我就想取消它的默认选中行.在DataGridView绑定数据之后加上了ClearSelection().这样一来,不论是启动窗体 ...
- JS焦点图 上下翻动 支持IE6
<div class="slider"> <ul class="num" id="homePushName"> &l ...
- Sublime Text 快捷键
ctrl+shift+t:重新打开最近关闭文件 快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t ...
- C# 平时碰见的问题【4】
1. 模糊查询 like的参数化写法 string keyword="value"; // 要模糊匹配的值 错误示范: sql: string strSql=" ...
- 【原创】StickHeaderListView的简单实现,解决footerView问题
1.前言: 前几天用了GitHub上se.emilsjolander.stickylistheaders这个组件,然后发现这个组件的listview不能添加footerView,加了footer后,滑 ...
- WPF动画 storyboard
<Window x:Class="StoryBoard.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- Collection、Iterator、Set、HashSet
Collection接口的基本方法 boolean add(Object o) 向集合当中加入一个对象 void clear() 删除集合当中的所有对象 boolean isEmpty() 判断集合是 ...