puzzle

Moving a bit within a byte using bitfield or bitwise operators

╄→гoц情女王★ 提交于 2019-12-23 09:50:55
问题 Is there an elegant way of moving a bit within a byte (or word/long). For simplicity, lets use a simple 8-bit byte and just one bit to move within the byte. Given a bit number, based on 0-7 Least-sig-bit to most-sig-bit, (or bits 1-8 if you'd rather), I would like to move a bit from one position to another: 7654 3210 <bit position 0101 1010 <some binary value --x- --y- <move bit from x to y 0111 0100 <new value with x moved to y and intervening bits shifted left So, x at bit position 5 moves

Wandering star - codeabbey task

三世轮回 提交于 2019-12-23 08:03:10
问题 I'm trying to solve this problem and I'm not sure what to do next. Link to the problem Problem statement: Suppose that some preliminary image preprocessing was already done and you have data in form of coordinates of stars on two pictures. These pictures are about 100x100 millimeters, and coordinates are also given in millimeters relative to their center. Look at the schematic reprsentation below: You see that in both pictures stars are shown in roughly circular area (think of it as of

What makes the following code print false?

倾然丶 夕夏残阳落幕 提交于 2019-12-22 11:26:16
问题 public class Guess { public static void main(String[] args){ <sometype> x = <somevalue>; System.out.println(x == x); } } i have to change sometype and somevalue so that it returns false? is it possible? 回答1: One: float x = Float.NaN; Two: double x = 0.0/0.0; Why? As mentioned here already, NaN is never equal to another NaN - see http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html So why this is not returning false? Float x = Float.NaN; The answer is that here, instead

Two generals' agreement

妖精的绣舞 提交于 2019-12-21 20:50:35
问题 I am trying to figure an agreement protocol on an unreliable channel. Basically two parties (A and B) have to agree to do something, so it's the two generals' problem. Since there is no bullet-proof solution, I am trying to do this. A continuously sends a message with sequence 1 When B receives seq 1 it continuously replies with sequence 2 At this point A receives seq 2 so it starts sending seq 3 ... My question. When can the two parties conclude that they can take the action ? Obviously I

i = ++i + ++i; in C++

与世无争的帅哥 提交于 2019-12-21 03:26:11
问题 Can someone explain to me why this code prints 14? I was just asked by another student and couldn't figure it out. int i = 5; i = ++i + ++i; cout<<i; 回答1: The order of side effects is undefined in C++. Additionally, modifying a variable twice in a single expression has no defined behavior (See the C++ standard, §5.0.4, physical page 87 / logical page 73). Solution: Don't use side effects in complex expression, don't use more than one in simple ones. And it does not hurt to enable all the

Project Euler problem 67: find maximum cost path in 100-row triangle

Deadly 提交于 2019-12-21 03:15:12
问题 In Project Euler's problem 67 there is a triangle given and it contains 100 rows. For e.g. 5 9 6 4 6 8 0 7 1 5 I.e. 5 + 9 + 6 + 7 = 27. Now I have to find the maximum total from top to bottom in given 100 rows triangle. I was thinking about which data structure should I use so that problem will get solved efficiently. 回答1: You want to store this as a directed acyclic graph. The nodes are the entries of your triangular array, and there is an arrow from i to j iff j is one row lower and

Skyscraper puzzle algorithm

筅森魡賤 提交于 2019-12-20 07:48:41
问题 I'm writing an algorithm to solve skyscrapers puzzles: Skyscraper puzzles combine the row and column constraints of Sudoku with external clue values that re-imagine each row or column of numbers as a road full of skyscrapers of varying height. Higher numbers represent higher buildings. To solve a Skyscraper puzzle you must place 1 to 5, or 1 to whatever the size of the puzzle is, once each into every row and column, while also solving each of the given skyscraper clues. To understand

Word Jumble Algorithm

妖精的绣舞 提交于 2019-12-19 16:22:22
问题 Given a word jumble (i.e. ofbaor), what would be an approach to unscramble the letters to create a real word (i.e. foobar)? I could see this having a couple of approaches, and I think I know how I'd do it in .NET, but I curious to see what some other solutions look like (always happy to see if my solution is optimal or not). This isn't homework or anything like that, I just saw a word jumble in the local comics section of the paper (yes, good ol' fashioned newsprint), and the engineer in me

Word Jumble Algorithm

南楼画角 提交于 2019-12-19 16:19:12
问题 Given a word jumble (i.e. ofbaor), what would be an approach to unscramble the letters to create a real word (i.e. foobar)? I could see this having a couple of approaches, and I think I know how I'd do it in .NET, but I curious to see what some other solutions look like (always happy to see if my solution is optimal or not). This isn't homework or anything like that, I just saw a word jumble in the local comics section of the paper (yes, good ol' fashioned newsprint), and the engineer in me

Not able to solve the puzzle regarding this code

安稳与你 提交于 2019-12-19 03:16:40
问题 int i,n=20; for(i=0;i<n;i--) printf("-"); I have been rattling my brain but have not been able to solve this. Delete any single character or operator from above code and the program should print "-" 20 times Please help! 回答1: I don't think you can do it by deleting a character, but I have three solutions that replace (well, one of them adds a character, but only because you have no whitespace in your program. If you had whitespace, it would replace a space). Solution 1 int i,n=20; for(i=0;-i