Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
suppose I have one array having the values:
array(20,40,30,15,60,50,10)
Now what i want is to I need to create bunch of 100 or near to hundred.and create separate round for each set of 100(or near to 100).
Say
Case 1:
Round 1: array(60,30,10) // 100 or near to 100
Round 2: array(40,50) // 100 or near to 100
Round 3: array(15,20) // 100 or near to 100 or remaining
Case 2:
Round 1: array(60,40) // 100 or near to 100
Round 2: array(50,20,30) // 100 or near to 100
Round 3: array(15,10) // 100 or near to 100 or remaining
So how can I achieve this?
Is there any algorithm regarding this that I can study?
You are describing the binpacking problem, which is NP-Complete, thus there is no known polynomial solution to solve it.
You can try an exponential approach (check all possibilities) if you need exact. If you want "close enough" you can search the literature for some approximation algorithm, or use some heuristic search solution from the AI field such as Genetic Algorithm or Hill Climbing.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In PHP source code function uniqid() have following C code:
(I removed some types to shorten it)
//...
struct timeval tv;
gettimeofday(&tv, NULL);
int sec = (int) tv.tv_sec;
int usec = (int) (tv.tv_usec % 0x100000);
// The max value usec can have is 0xF423F,
// so we use only five hex digits for usecs.
printf("%08x%05x", sec, usec);
//...
If we put criticism aside, they try to produce 64 bit timestamp.
0xF423F is probably CLOCKS_PER_SEC - 1 (CLOCKS_PER_SEC is decimal 1000000),
but where this 0x100000 come from and what could be the reason to use modulus instead of bitwise and?
She or he could write the Unique ID as printf("%08x%08x", sec, usec)
sample output:
55189926000eb16f
5518997900051219
5518997a0005171b
The zeros in position 8 to 10 are consistent, they don't add entropy, so he wants to get rid of those zeros. The new UID will be 3 bytes shorter with the same entropy. He could simply use printf("%08x%05x", sec, usec);
sample output:
55189926eb16f
5518997951219
5518997a5171b
But that's on the assumption that usec is guaranteed to be less than 0x100000 otherwise UID will be up to 16 bytes long. You need % 0x100000 for insurance. It's also the same as & 0xFFFFF. Technically the insurance should be % 1000000 (decimal), but it doesn't really matter, it's still the same entropy.
Or we could just use the 16 byte version because saving 3 lousy bytes don't matter these days.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm building a browser game which you can shoot missiles on factories.
This is example of a similar SQL table:
user_ID iron steel metal gold
1 21 30 39 25
2 7 10 25 50
I need each missile to destroy 1 factory.
For example I shoot 10 missile, so 10 factories are destroyed. But I need it to have completely randomly and even.
The problem is if according to this SQL table, if I shoot for example 40 missile on user_ID '2'.
In best case I destroy 10 factories of each kind. But how can I do it when I don't know if there is enough of each kind?
First, divide the number of missiles by the number of factory types that have at least 1 factory. If all the types have at least this many factories, subtract this from each column.
If any of the types have less than this, they'll be totally wiped out. Reduce them to 0 and subtract their original totals from the number of missiles. Then start again, using just the remaining missiles.
Repeat this until you use up all the missiles or all factories are wiped out.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a voting table called product_reviews, with a field that is ENUM from 1 to 5 called rating.
I want to get average rating from all rows with product_id 1665. I am using a star rating system so the average cannot be with decimals. Must be a number from 1 to 5.
Thank you very much.
SELECT round(avg(rating)) as average_review,
count(rating) as number_of_reviews
FROM product_reviews
WHERE product_id = 1665
You need to use round here since avg will return a value between 1.0 and 5.0, and if you use floor or ceil you're effectively eliminating 1 or 5 from the possible results unless all reviews on the product have that score.
Also, you'll alienate your users if they can openly see a product got 50 reviews with 1 star, and 1 review with 2 stars, and you're showing an average score of 2 stars next to that. It'll make your site seem unreliable which is usually not a good thing for a site that contains reviews. For this reason most star rating systems are also capable of showing partial stars to make it more precise.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
3rd grade question:
How do you calculate the distance between two points on a flat surface?
I have been going through the Google_Results and it seems everything i find applies to Long/Lat and not a flat surface.
I'm working on making ObjectA choose between ObjetsC,D,E... , select the closest one and move toward it. So I have to loop through my SQL table, pull out what's in range, and loop through the results to calculate distances.
Any help with this math I haven't had to remember in years would be appreciated.
You will need to use a Euclidean distance formula (specifically for 2 dimensions). Basically the formula is:
d = sqrt((x1 - x2)^2 + (y1 - y2)^2)
d is your final distance
sqrt is the square root (the sqrt() function in PHP)
x1 and y1 are your x-y coordinates for your first point
x2 and y2 are your x-y coordinates for your second point
^2 means raise to the second power (use the pow() function for PHP)
Pythagoras was the Greek philosopher who developed the Pythagorean Theorem, which can be used to derive the 2-dimensional distance formula (which is different from Euclid's derivation).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a bit of a puzzle. Look at the sum and result below:
$markTotal += ($session['Mark'] / 100 * $session['SessionWeight']);
Result on Browser:
Module: CHI2550 - Modern Database Applications 41.2 (this is $markTotal)
Session: AAB 72(this is $session['Mark']) 20% (this is $session['SessionWeight'])
Session: AAE 67(this is $session['Mark']) 40% (this is $session['SessionWeight'])
As you can see the answer to the calculation above is right, the answer is 41 .2 as it adds up the two session marks, divide by 100 and then times it by the total amount of the percentage.
But I want to include a total mark where except it is out of the total session percentage (60% for above example), it is out of a 100% but I can not work as simple as that as in above example one session is worth more than other. I have worked out that the answer for the total mark of the above example out of 100% should be 69, but how do I achieve this in my calculation.
Thank you and any help is much appreciated :)
You'd need to add up the total marks as well. So if session AAB has 90 total marks available (and the student got 72) and AAE has 80 marks (and got 67) then it'd be
(72) + (67) 14.4 26.8 41.2
(--) * 0.2 + (--) * 0.4 = ---- + ---- = ---- = 79.23%
(90) + (80) 18 34 52