find a multiple
Description
Input
Output
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input
5
1
2
3
4
1
Sample Output
2
2
3
题目大意是: 输入n个数,从中找出数字,它们加起来之和是n的倍数,并将它们输出。 分析:之和是n的倍数,就是对n求余是0。 通常大家会想把这些数随机组合,得到的和存放在一个数组里,但遇到很多数,这就不好办了。 由于问题只要找出一组即可,所以我们就是要找到一组。 所以我建立个抽屉模型,
为什么可以用抽屉模型?有n个数,这些数的连续和分别对n取余得到的余数范围是0~n-1, 不相同的余数个数不超过n,当个数为n时, 那其中肯定有一组数的和满足条件,若小于n,这当中至少有两组数的余数相同,举个
例子,1,3,5,9,2 ,5个数, 我把它们连续相加变成, 1,4, 9, 18, 20, 仍然是5个数。 使他们一一对5取余,得到 1, 4, 4, 3, 0。 我把余数当作抽屉,和作为物体, 这里有5个物体,4个抽屉,
第一组数是1,3,第二组数是1,3,5。 用后者减前者便是答案。
#include <stdio.h>
#define M 10001
int a[M], mod[M];
int main()
{
int n, i, sum, begin, end;
while(scanf("%d", &n) != EOF){ sum = ;
for(i = ; i <= n; i++){ scanf("%d", &a[i]); }
for(i = ; i <= n; i++){ sum = (sum + a[i]) % n;
if(!sum)
{
begin = ;
end = i;
break;
}
else if(mod[sum] > )
{
begin = mod[sum] + ;
end = i;
break;
}
mod[sum] = i; }
printf("%d\n", end - begin + );
for(i = begin; i <= end; i++){ printf("%d\n", a[i]); }
putchar('\n'); }
return ;
}
find a multiple的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- scala - multiple overloaded alternatives of method bar define default arguments
同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
- OPEN CASCADE Multiple Variable Function
OPEN CASCADE Multiple Variable Function eryar@163.com Abstract. Multiple variable function with grad ...
随机推荐
- JVM参数调优
JVM参数调优 JVM参数调优是一个很头痛的问题,可能和应用有关系,下面是本人一些调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2CPU,4G内存,d ...
- java 中 静态块的作用
(一)java 静态代码块 静态方法区别一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序 ...
- JQuery解析json数据
<script> var data ="{" + "root:[" + "{name:'1',value:'0'}," + &q ...
- 利用K-means聚类分类,进行特征学习
这只是老师安排的一个实验,准备过程中遇到各种问题,现在贴出来供大家参考,是Andrew Ng参与的研究, 论文依据如下,第二篇是一篇相关的论文, Learning Feature Representa ...
- TestNg线程池配置、执行次数配置、超时配置
使用注解的方式对TestNg线程池配置.执行次数配置.超时配置 注:使用注解来控制测试方法运行的次数和超时时间,timeOut在单线程或者多线程模式下都可用,threadPoolSize设置了线程池的 ...
- HTML图片元素(标记)
<html> <head> <title>第一个网页</title> </head> <body> ************** ...
- 测试你是否和LTC水平一样高[HDU1407]
测试你是否和LTC水平一样高Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 解决iPhone中overflow:scroll;滑动速度慢或者卡的问题
在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...
- eclipse安装color theme插件
为Eclipse添加Color.Theme的插件 这样可以方便一键更换主题,再也不用一个一个设置BackgroundColor了,同时也方便回退到default默认主题配置. 方法一: 打开Eclip ...
- webstorm常用快捷键(常用)
ctrl+/ 注释 ctrl+shift+/ 注释一块的代码 ctrl+shift+z 返回撤撤销前的操作 ctrl+shift+up/down 代码向上/向下移动 ctrl+b或ctrl+鼠标左键单 ...