标签:make minimum new task nts str 现在 strong not
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 .
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 .
1- minimumNumber ({3,1,2}) ==> return (1)
2- minimumNumber ({2,12,8,4,6}) ==> return (5)
3- minimumNumber ({50,39,49,6,17,28}) ==> return (2)
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! } }
标签:make minimum new task nts str 现在 strong not
原文地址:https://www.cnblogs.com/ukzq/p/9384927.html