puzzle

Place random non-overlapping rectangles on a panel

倾然丶 夕夏残阳落幕 提交于 2019-11-26 22:54:23
问题 I've a panel of size X by Y. I want to place up to N rectangles, sized randomly, upon this panel, but I don't want any of them to overlap. I need to know the X, Y positions for these rectangles. Algorithm, anyone? Edit : All the N rectangles are known at the outset and can be selected in any order. Does that change the procedure? 回答1: You can model this by a set of "free" rectangles, starting with single one with coordinates of 0,0, size (x, y). Each time you need to add one more rectangle,

How to find the exact set of operations for a specific number?

随声附和 提交于 2019-11-26 22:05:41
问题 I am trying to implement a program that answers this problem : if you are given a specific number (for example : 268) and another 6 numbers (for example : 2,4,5,25,75,100) How can I find the operation that gives me the exact answer or the closest one to it? You can answer the previous example by using this operation : 75*4-25-5-2 = 268 Rules : you can use these arithmetic operations : +, -, *, / , (). when you use division the reminder must be equal to 0 (6/3 is ok but 6/4 is not ok!). you

Efficiently reverse the order of the words (not characters) in an array of characters

烂漫一生 提交于 2019-11-26 21:42:39
Given an array of characters which forms a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Example input and output: >>> reverse_words("this is a string") 'string a is this' It should be O(N) time and O(1) space ( split() and pushing on / popping off the stack are not allowed). The puzzle is taken from here . Thomas Watnedal A solution in C/C++: void swap(char* str, int i, int j){ char t = str[i]; str[i] = str[j]; str[j] = t; } void reverse_string(char* str, int length){ for(int i=0; i<length/2; i++){ swap(str, i, length-i-1); } } void

Algorithm to determine if array contains n…n+m?

久未见 提交于 2019-11-26 21:21:52
I saw this question on Reddit, and there were no positive solutions presented, and I thought it would be a perfect question to ask here. This was in a thread about interview questions: Write a method that takes an int array of size m, and returns (True/False) if the array consists of the numbers n...n+m-1, all numbers in that range and only numbers in that range. The array is not guaranteed to be sorted. (For instance, {2,3,4} would return true. {1,3,1} would return false, {1,2,4} would return false. The problem I had with this one is that my interviewer kept asking me to optimize (faster O(n)

why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java?

送分小仙女□ 提交于 2019-11-26 21:15:17
问题 why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java? 回答1: Because the multiplication overflows 32 bit integers. In 64 bits it's okay: public class Test { public static void main(String[] args) { int intProduct = 24 * 60 * 60 * 1000 * 1000; long longProduct = 24L * 60 * 60 * 1000 * 1000; System.out.println(intProduct); // Prints 500654080 System.out.println(longProduct); // Prints 86400000000 } } Obviously after the multiplication has overflowed, the

Compile time sizeof_array without using a macro

一曲冷凌霜 提交于 2019-11-26 20:14:13
This is just something that has bothered me for the last couple of days, I don't think it's possible to solve but I've seen template magic before. Here goes: To get the number of elements in a standard C++ array I could use either a macro (1), or a typesafe inline function (2): (1) #define sizeof_array(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0])) (2) template <typename T> size_t sizeof_array(const T& ARRAY){ return (sizeof(ARRAY)/sizeof(ARRAY[0])); } As you can see, the first one has the problem of being a macro (for the moment I consider that a problem) and the other one has the problem of not

How to mix two ARGB pixels?

北战南征 提交于 2019-11-26 17:39:25
问题 How can I mix two ARGB pixels ? Example Here A is (Red with Alpha) and B is ( Blue with Alpha ). 回答1: Taken from the same Wikipedia article where you got the image: Translating to values which range from 0 to 255: rOut = (rA * aA / 255) + (rB * aB * (255 - aA) / (255*255)) gOut = (gA * aA / 255) + (gB * aB * (255 - aA) / (255*255)) bOut = (bA * aA / 255) + (bB * aB * (255 - aA) / (255*255)) aOut = aA + (aB * (255 - aA) / 255) 回答2: It seems like this is what you want: http://en.wikipedia.org

Eric Lippert's challenge “comma-quibbling”, best answer?

最后都变了- 提交于 2019-11-26 17:38:58
问题 I wanted to bring this challenge to the attention of the stackoverflow community. The original problem and answers are here. BTW, if you did not follow it before, you should try to read Eric's blog, it is pure wisdom. Summary: Write a function that takes a non-null IEnumerable and returns a string with the following characteristics: If the sequence is empty the resulting string is "{}". If the sequence is a single item "ABC" then the resulting string is "{ABC}". If the sequence is the two

How to test randomness (case in point - Shuffling)

别来无恙 提交于 2019-11-26 17:31:39
First off, this question is ripped out from this question. I did it because I think this part is bigger than a sub-part of a longer question. If it offends, please pardon me. Assume that you have a algorithm that generates randomness. Now how do you test it? Or to be more direct - Assume you have an algorithm that shuffles a deck of cards, how do you test that it's a perfectly random algorithm? To add some theory to the problem - A deck of cards can be shuffled in 52! (52 factorial) different ways. Take a deck of cards, shuffle it by hand and write down the order of all cards. What is the

Programming Riddle: How might you translate an Excel column name to a number?

蓝咒 提交于 2019-11-26 17:30:35
问题 I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It's about translating Excel column letters to actual numbers, if you recall, Excel names its columns with letters from A to Z, and then the sequence goes AA, AB, AC... AZ, BA, BB, etc. You have to write a function that accepts a string as a parameter (like "AABCCE") and returns the actual column number. The solution can be in any language. 回答1: I wrote this ages ago for