寻找List之和的最近素数
Task :
Given a List [] of n integers , find minimum mumber to be inserted in a list, so that sum of all elements of list should equal the closest prime number .
Notes
List size is at least 2 .
List's numbers will only positives (n > 0) .
Repeatition of numbers in the list could occur .
The newer list's sum should equal the closest prime number .
Input >> Output Examples
1- minimumNumber ({3,1,2}) ==> return (1)
Explanation:
- Since , the sum of the list's elements equal to (6) , the minimum number to be inserted to transform the sum to prime number is (1) , which will make the sum of the List equal the closest prime number (7) .
2- minimumNumber ({2,12,8,4,6}) ==> return (5)
Explanation:
- Since , the sum of the list's elements equal to (32) , the minimum number to be inserted to transform the sum to prime number is (5) , which will make the sum of the List equal the closest prime number (37) .
3- minimumNumber ({50,39,49,6,17,28}) ==> return (2)
Explanation:
- Since , the sum of the list's elements equal to (189) , the minimum number to be inserted to transform the sum to prime number is (2) , which will make the sum of the List equal the closest prime number (191) .
public class Solution
{
public static int minimumNumber(int[] numbers)
{
//首先求得现在list中的数值之和
int sumOri = 0; for(int i=0;i<numbers.length;i++){
sumOri += numbers[i];
}
//用另一个数对原始和进行递增
int sumNow = sumOri;
boolean searchPrime = true;
//do...while递增原始数值,并判定是否为素数
while(searchPrime)
{ int count = 0;
for(int i=2;i<=Math.floor(sumNow/2);i++){
if(sumNow%i==0){
//合数进入
count++;
}
} //count为0,这个数是质数,停止查找
if(count == 0){
searchPrime = false;
}else{
//递增新的和
sumNow++;
} }
return sumNow-sumOri; // Do your magic!
}
}
寻找List之和的最近素数的更多相关文章
- 基于visual Studio2013解决C语言竞赛题之1085相邻之和素数
题目 解决代码及点评 /************************************************************************/ /* ...
- HDU 1016 Prime Ring Problem (素数筛+DFS)
题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...
- HDU 4548 美素数(打表)
HDU 4548 美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...
- 基于visual Studio2013解决C语言竞赛题之0501挑选素数
题目
- 数学#素数筛法 HDU 4548&POJ 2689
找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...
- 【DFS】素数环问题
题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...
- java高效判断素数
java高效判断素数 package solution; public class Prime { // 偶数可以由有两个素数相加得到, 一个偶数可能有多个这样的两个素数, 请寻找到 这样两个素数,让 ...
- 【算法】C语言趣味程序设计编程百例精解
C语言趣味程序设计编程百例精解 C/C++语言经典.实用.趣味程序设计编程百例精解(1) https://wenku.baidu.com/view/b9f683c08bd63186bcebbc3c. ...
- 网络流 I - Fox And Dinner CodeForces - 510E
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
随机推荐
- Android 判断是否是Rtl
第一种方法: private boolean isRtl() { return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) ...
- Android 内存使用hprof文件打开方法
http://blog.csdn.net/moruihong/article/details/7677128 与C++的内存不同,C++的内存泄露是由于分配了内存给某程序但是又没有回收造成的.Java ...
- hdu5009 Paint Pearls[指针优化dp]
Paint Pearls Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 【BZOJ2668】[cqoi2012]交换棋子 费用流
[BZOJ2668][cqoi2012]交换棋子 Description 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列 ...
- 为listview的item中的元素设置onclick事件
表达能力比较差,所以现在解释一下标题的意思:listview的列表项,点击的时候触发的是itemOnClick事件,点击后转向到A页:那么,假如在子项中有一个连接是想转到B页,我们该怎么办呢.这样能明 ...
- SpringBoot SpringApplication底层源码分析与自动装配
目录 抛出问题 @SpringBootApplication注解剖析 SpringApplication类剖析 第一步:配置SpringBoot Bean来源 第二步 :自动推断SpringBoot的 ...
- JS事件监听的添加方法
一. 我们一般在的事件添加时是这样做的: elm.onclick = function( ) { //handler } 这样的写法兼容主流的浏览器,但是存在一个问题,当同一个elm绑定多个事件时,只 ...
- 无线路由器wan口和lan口ip同网段导致无法上网解决办法
环境 本地网段为192.168.0.0/24 路由器默认网段也是192.168.0.0/24 设置好路由器wan口DHCP自动获取ip以后无法上网 解决办法 把路由器是lan口地址设置为192.168 ...
- 解析导航栏的url
前段时间做ui自动化测试的时候,导航栏菜单始终有点问题,最后只好直接获取到url,然后直接使用driver.get(url)进入页面: 包括做压测的时候,比如我要找出所有报表菜单的url,这样不可能手 ...
- java web 打印(lodop)案例
应需求接触到lodop 打印. 首先在lodop官网下载相关文件(js.css等):http://www.lodop.net/download.html 在下载好的包里 除了html页面 其他的j ...