Home > Back-end >  Give a bosses, and a algorithm
Give a bosses, and a algorithm

Time:11-22

If there is a number 10000, ranging from 1 to 100, request according to each group of more than 100 groups, each group of number is not more than 80, will these several successful group as far as possible, and see what method


Each group of more than 100 refers to the number added up to more than 100 of the same group

CodePudding user response:

You said the demand is not very clear, when there is a extreme to do,

1 (assuming that the 10000 number, all of them are actually only need the number of more than a certain number 1), then you that no matter how, can't satisfy all the requirements, and is as far as possible with less as far as possible with more group or group?
Assume that rule out this kind of situation, you must choose a conditional priority in the two conditions, must the meizu above 100, for example, or must be no greater than 80 number, that this problem is relatively simple,

I have to meet it is assumed that each group of more than 100 priority, to meet the conditions on the basis of the number for each group of no more than 80, use less as far as possible the group

 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

Public class Test1 {

Public static Boolean isFull (List The list) {
int sum=0;
For (int I: list) {
sum +=i;
}
If (sum & lt; 100) {
return false;
}
If (a list. The size () & gt; {
=80)return true;
}
return false;
}

Public static void main (String [] args) {
The Random Random=new Random ();
Int [] randomArray=new int [10000].
for (int i=0; I & lt; RandomArray. Length; I++) {
RandomArray [I]=random. NextInt (100) + 1;
}
List List=new ArrayList (a);
For (int I: randomArray) {
Boolean addFlag=false;
For (List List2: list) {
if (! IsFull (list2)) {
List2. Add (I);
AddFlag=true;
break;
}
}
if (! AddFlag) {
List List2=new ArrayList (a);
List2. Add (I);
List. The add (list2);
}
}

System. The out. Println (list. The toString ());
}
}



Application performance is also can do some optimization, but saw a total of 10000 data, not optimize also so return a responsibility,

Algorithm also can be optimized, can forecast the current data in advance to join after each change effect, or can be exchanged between the organization, but the problem will become more complicated, if want to seek the optimal solution, then the problem will be too complicated, I make this just a try to meet the requirements and the balance between performance, rather than an optimal solution,

CodePudding user response:

Saw a topic and rumlee analysis of the building Lord, let me talk about my opinion on this topic, it was interesting topic, I don't think need considering those doubts, rumlee students may think that this is an unknown array, you have to do is to judge whether it can meet the requirements, if you do not meet the reason, if satisfied, how points,
1, first consider each group of 80 number (not necessarily the same number in each group, consider the first, the simplest case), that is 125 array, and 100/80=1.25, that is to say, if the number of 10000, the 1, the number of less than 60 (20 * 60 * 1 + 2)=100, then how to points, if 1 is greater than or equal to the number of 60, then to separate 1, ensure that the number of each group 1 is less than 60 is ok, but can lead to a condition here, if the amount of data that is greater than 1 is equal to the number of 60 and 125 * 2 greater than or equal to 125 * 20, how the points are not,
2, then if you need to produce each group number less than 80 [premise to submit is 1 less than the number of the number of 60 and 125 * 2 greater than or equal to 125 * 20], we in 70 a set number for example, 100/70 integer or 1, if 1 is less than 40, the number of is how can, in other words is 1 * x + 2 * y more than equal to 100, and x + y=80 cases the value of x,
3, the analysis here, we can know, how the 10000 data points, main is to see 1, 2, 3, 4... 100 each number, the number of distribution assumption: the priority 1, 2 points, then the rest of the guarantee each group of not less than the number 34, is certainly more than 100 (3 * 34=102), assuming that the distribution of 1, 2, 3 are ok, then the back of the array number is not less than 25 in each group must meet the requirements,

CodePudding user response:

Above a place is wrong, should be 1 if the amount of data that is greater than or equal to the number of 60 and 125 * 2 is greater than the number of 10000-1-125, how the points are not,

CodePudding user response:

Feel your topic to be not rigorous, not too well, bad writing algorithm,
  • Related