2010-10-28, 04:38 AM (This post was last modified: 2010-10-28, 12:24 PM by Kalovale.)
Well, I'm thinking I should form a good coding habit from the beginning. There is almost no complexity in these problems, but I feel that there are better ways to write the codes, or even more condensed logical approaches. So, if you're bored, would you kindly show me how you'd tackle these problems, I'll figure out the details on my own.
Current issue: I think I'm using if-else-then statements too excessively.
codingbat:
Logic:
1/
Spoiler
[COLOR="red"]We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops.
[COLOR="red"]Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.
[COLOR="red"]Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
public int luckySum(int a, int b, int c) {
if(a==13){return 0;}
else{if(b==13){return a;}
else{if(c==13){return a+b;}
else{return a+b+c;}}}
}
4/
Spoiler
[COLOR="red"]Given three ints, a b c, return true if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: Math.abs(num) computes the absolute value of a number.