poj 23565-Find a multiple
Find a multiple
The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input
The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output
In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.
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的倍数。
第二种sum[1]=a[1],sum[2]=a[1]+a[2],…
其中有n的倍数,输出,就好了。
第三种 有sum[]中有n个数,sum%n的范围是0~n-1,这里就用到了抽屉原理,有n个数字,范围在1~n-1, 必定存在 sum[i]%n-sum[j]%n==0;
就可以知道有i+1~j
代码有点乱,,,,,
#include<stdio.h>
int a[10010],sum[10010];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sum1=0;
int s;
int flag=0;
int k;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]%n==0)
{
k=a[i];
flag=1;
}
sum1+=a[i];
sum[i]=sum1;
}
if(flag==1)
{
return printf("1\n%d\n",k);
}
else
{
int d=0;
for(int i=1;i<=n;i++)
{
if(sum[i]%n==0)
{
printf("%d\n",i);
for(int j=1;j<=i;j++)
printf("%d\n",a[j]);
d=1;
}
}
if(d==0)
{
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
if((sum[j]%n-sum[i]%n)==0)
{
printf("%d\n",j-i);
for(int k=i+1;k<=j;k++)
printf("%d\n",a[k]);
break;
}
}
}
}
}
}
return 0;
}
poj 23565-Find a multiple的更多相关文章
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- DFS/BFS(同余模) POJ 1426 Find The Multiple
题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 1426 Find The Multiple(数论——中国同余定理)
题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...
- POJ 1426 - Find The Multiple - [DP][BFS]
题目链接:http://poj.org/problem?id=1426 Given a positive integer n, write a program to find out a nonzer ...
- poj 1426 Find The Multiple (bfs 搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18012 Accepted: 729 ...
- [深度优先搜索] POJ 1426 Find The Multiple
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28550 Accepted: 118 ...
随机推荐
- C++中的引用和指针
引用和指针有何区别?何时只能使用指针而不能使用引用? 引用是一个别名,不能为 NULL 值,不能被重新分配:指针是一个存放地址的变量.当需要对变量重新赋以另外的地址或赋值为 NULL 时只能使用 ...
- js统计字符出现次数
var s = "The rain in Spain falls rain mainly in the rain plain"; var reg = new RegExp(&quo ...
- java8Stream map和flatmap的区别
map和flatmap的区别 map只是一维 1对1 的映射 而flatmap可以将一个2维的集合映射成一个一维,相当于他映射的深度比map深了一层 , 所以名称上就把map加了个flat 叫flat ...
- 面向对象(OOP)三
一.面向对象基础原则 1)单一职责原则(类要写得小而精,低耦合) 内部类 单列模式 对于单一职责原则,其核心思想为:一个类,最好只做一件事,只有一个引起它的变化.单一职责原则可以看做是低耦合.高内聚在 ...
- 属性,选择器和css
一.属性 属性:表示事物的一些特征 属性分为标签属性和样式属性 标签属性:<img src="1.jpg" width="200px" heifht=&q ...
- Canny边缘检测学习
Canny边缘检测学习:http://www.open-open.com/lib/view/open1453460512558.html 高斯滤波学习:http://www.cnblogs.com/q ...
- Bootstrap插件-collapse
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- Spring @Autowired使用介绍
参考博客: https://blog.csdn.net/u013412772/article/details/73741710 引用文章地址: https://my.oschina.net/Helio ...
- https验证新发现-老知识
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 可以设置https全局的域名校验规则 HttpsURLConnecti ...
- javascript设计模式之中介者模式
/* * 小游戏演示中介者模式 * Home 按键 1 * Guest 按键 0 * 半分钟内看谁按下的次数多 * * 参与的对象: * 玩家 * 计分板 * 中介者 * * 中介者模式使对象之间松耦 ...