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. PUTTY使用Ctrl+s僵死的问题

    算是分享个小经验吧! 一直都是使用VM+PUTTY的方式调试Linux程序,有时候在Vi中编辑了程序,Windowns下旧习难改,顺手就Ctrl+s了,尽管我知道Vi的保存是:w. 很不幸这时的PUT ...

  2. 【SMS】移动短信网关返回信息状态代码说明【China Mobile】

    1 由SMSC返回的一般结果状态报告 含义 说明 处理建议DELIVRD 消息发送成功 用户成功接收到短信 ??EXPIRED 因为用户长时间关机或者不在服务区等导致的短消息超时没有递交到用户手机上 ...

  3. jquery animate 动画效果使用说明

    animate( params, [duration], [easing], [callback] ) 用于创建自定义动画的函数. 这个函数的关键在于指定动画形式及结果样式属性对象.这个对象中每个属性 ...

  4. SQL SERVER中查询参数为空(null)时默认查询所有的实现

    最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...

  5. decode行转列,case when,

    1.行转列 转之前:

  6. 生产WCF客户端类文件的命令格式

    生产WCF客户端类文件的命令格式: svcutil.exe net.tcp://127.0.0.1:8732/ChromaMI.Remote.ConfigService/RemoteConfigSer ...

  7. 谈谈final、finally、finalize的区别

    1.final:如果一个类被final修饰,意味着该类不能派生出新的子类,不能作为父类被继承.因此一个类不能被声明为abstract,又被声明为final.将变量或方法声明为final.可以保证他们在 ...

  8. STM32F0xx_PWR低功耗配置详细过程

    Ⅰ.概述 今天总结PWR部分知识,请看“STM32F0x128参考手册V8”第六章.提供的软件工程是关于电源管理中的停机模式,工程比较常见,但也是比较简单的一个实例,根据项目的不同还需要适当修改或者添 ...

  9. Windows上搭建android开发环境

    在搭建android开发环境时需要四部分内容,框架如下 其中Java SDK和Eclipse在java4android中有过介绍,重点介绍ADT和Android SDK的安装. 安装Android S ...

  10. Unix 编程

    1. Users and Groups 真实用户ID和真实组ID 真实用户ID和组ID表示运行进程的真实用户 ID 和 组ID. 有效用户ID和有效组IDp 有效 ID 是进程进行相关操作(比如系统调 ...