It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm very new to java, so i need a help . here is my problem:
I have two weights 1gram and 5 gram. now user will give the weight he/she wants to calculate, and the user will also give the count for 1gram and 5 gram. the program will return true if it can calculate otherwise it will return false. I'm giving you a example:
count for 1 gram is 5(say)
count for 5 gram is 3(say)
weight to measure the 12 gram
by using two 1gram and two 5gram i can make 12gram. so it will return true.
please help me to do this.
Thanks in advance.
As this is homework so here are hints for the algorithm for solution
Addition way
First you should take out multiples of 5 from the weight to be measured.
Remainder weight should be equal to number of 1 gram weights.
For example:
When input is 3x5gm and 2x1gm and you want to weigh 12gms
12 / 5 = 2 (5 gram weights)
12 % 5 = 2 (1 gram weights)
If these numbers are less than number of weights you have you have the answer.
Subtraction Way
First you should take out multiples of 5 from the weight to be measured.
For example:
When input is 3x5gm and 2x1gm and you want to weigh 13gms
13 / 5 = 2 (5 gram weights)
13 % 5 = 3 (1 gram weights)
Since you don't have enough weights to do so then you can look if you can manage the remainder with 1 additional 5gm and remaining 5-1gm weights i.e. 1x5gm and 5-3=2x1gm weights so the answer will be 3x5gm and 2x1gm
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So, I have created a program that runs in two parts. Firstly, I generate a list of 500 random numbers and store them in a text file. Secondly, I am required to find the max, min, average, etc. However, I am also supposed to find how many consecutive numbers come up. This is where my problem lies. I am not sure how to approach it.
Looking for a place to start.
Thank you
As you go along the array you will keep the list of the following things: current largest seen element, current smallest seen, total of all elements seen so far, ..., the last element seen & total count of consecutive numbers. If current number == last element seen + 1, you increase the consecutive count. There should also be a flag for the first element in the consecutive sequence, so that, say, {1, 2} counted 2 occurrences instead of one
last_seen = -1
previous_consecutive = false
for(x in numbers_list):
if x == last_seen + 1:
if not previous_consecutive:
number_of_consecutive_elements += 1
number_of_consecutive_elements += 1
previous_consecutive = true
else:
previous_consecutive = false
last_seen = x
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i need help in solving the following test question (it's not a homework or assignment)
the user can input from the console values from 1-9 and the program accepts 3 numbers one number per line.
if the user inputs the following:
3
2
4
the output should be:
1
2 2
3 3 3
1
2 2
1
2 2
3 3 3
4 4 4 4
i don't have any experience with trees, so please advise about what kind of tree is that and where can i start to accomplish the above program (i need some hints and advises that will help me to make this program)
thanks in advance.
No tree data structure is required. The general features of your program would be the following:
Read the three inputs
For each input n, use a for loop from 1 to n to print n lines using n as the output values
(The bit harder part:) On each line, you will also need to generate the required spacing. This will involve a calculation on your part based on n and the max value of n. Since n is a single digit, you won't have to take into account n taking up more characters once it's >= 10.
Define a 2D char array that will hold the digits. Start from the top middle. Use recursion.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need help thinking up a formula for finding the factors of a number:
Write a method named printFactors that accepts an integer as its
parameter and uses a fencepost loop to print the factors of that
number, separated by the word " and ". For example, the number 24's
factors should print as:
1 and 2 and 3 and 4 and 6 and 8 and 12 and 24
You may assume that the number parameter's value is greater than 0.
Please don't give me a COMPLETE program as I would like to try it out myself.
The current code I have has a for loop to control the number of "and's" appearing, however, I printed out the last number by itself since I don't want a "24 and" attached to it...
So the output looks something like this at the moment:
"1 and 2 and 3" (I haven't yet thought up the equation hence the 1,2,3...)
I'm currently thinking that the factors requires a % kind of formula right? Will I need division? I was also thinking of printing out 1 and whatever the number (in this case, 24) you are finding factors for, since 1 and the number itself are always factors of itself.
What else am I missing??
Thanks in advance!! :)
I'm currently thinking that the factors requires a % kind of formula right?
Yes.
I was also thinking of printing out 1 and whatever the number (in this case, 24) you are finding factors for, since 1 and the number itself are always factors of itself.
If you test every number from 1 to n (e.g. from 1 to 24) then 1 and the number itself don't need to be special cases (because they'll simply satisfy your ordinary "% kind of formula").
Maybe 1 is a special case because it doesn't have the word "and" in front of it.
What else am I missing??
This may be more complicated than you want, but to find all the factors of n you only need to loop up to the square root of n.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Prove that at least 1 and at most 2*k - 1 inversions are removed
I don't understand what does it mean by "inversions are removed", and I am not sure where to start.
If the list is 1, 3, 2, 4 and it's changed to 1, 2, 3, 4 then you've removed an inversion.
Obviously you've removed at least 1 inversion because a[i] and a[i+k] were out of order. At most you remove 2*k - 1 because if a[i] was bigger than a[i+1], ... ,a[i+k-1] then you fix k-1 inversions. Same for a[i+k] being smaller than all below it. Thus the most you can have is k-1 + k-1 + 1 (the last 1 being the one we counted already) which equals 2k - 1
Example: 1,10,3,4,5,6,7,8,9,2 -> switch a[2] with a[10], k = 8, 2 is now smaller than 10, and 2 is also smaller than 3-9 which is 7 numbers. Further 10 is now bigger than 3-9 which is, again, 7 numbers, so the improvement is 7 + 7 + 1 = 15 = 2*8 - 1
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to check to see if a player can no longer make moves but i cant seem to figure out how to check for it.
Say i have 5 tiles or less and each tile has a value between 1 and 3 on it. I want to run through the tiles and check if there are any possible combinations that will add up to 10. Its not as simple as just checking the total because there could be 3 tile with 3's on them and one with a 2. I have spent hours trying to figure this out....
Any ideas on how i could check for all possible combinations? I only need to see if one combination is possible. As soon as one is found i would break the loop to reduce the number of checks.
Edit: If it helps you can kinda see what im talking about if you download it. Its Numero: 21 on the android.
The object of the game is to add tiles to 21. So when the user gets down to a few tiles, sometimes a combination of 21 cant be acheived even when the total of all the tiles is above 21 because the numbers dont add up to exactly 21. This creates a problem because i am unable to check for that and tell the user that they have lost.
I really dont even know where to begin when it comes to checking for it. i can loop through all the tiles multiple times but the thhing is a combination could be as many tiles are on the board. So i have to check for combinations of 3, then 4 , then 5 and so on until the number of tiles left is reached. Its kind of hard for me to explain this exactly
Corrective Edit For Future Reference:
I apologize for my initial question for being so ambiguous. Happened to be the first question I ever asked on here... This is a much better description of the problem and the solution. I decided to keep the previous text for record.
The game has numerous numbered tile and I you have to add them up to 21 to remove. You can not go over 21. It has to be exact.
What I was wanting to check is if there was still a combination of tiles that could be used to add up to 21 exactly. A basic sum check doesn't work because you could have 5 number 5 tiles and be more than 21 but not possible to eliminate anymore.
Solution
As #mellamokb answered subset sum recursion needed to be used. Basically you loop through the tiles and on each tile you call the same function twice. One call adds the current tile and the other continues to the next iteration without adding the current tile. If any return true then the function is true. Basically a binary tree.
Code
boolean validate(tiles, index, subtotal, total){
if index >= tiles.length return false;
if subtotal == total return true;
return validate(tiles, index + 1, subtotal + tiles[index].number, total) ||
validate(tiles, index + 1, subtotal, total);
}
Call it with
validate(tiles, 0, 0, 21)
That about does it.
This sounds like a variation of the subset sum problem. The simplest algorithm is O(2^n), iterating over every possible subset combination by using bit flags, for example.
for i = 0 to 2^n - 1
set subtotal = 0
for each bit in i
if bit i is set, add ith element to subtotal
check subtotal against desired total (i.e., 10)
Or, alternatively using recursion:
validate(set, index, subtotal, total)
if index >= set.length return false;
if subtotal == total return true;
return validate(set, index + 1, subtotal + set[index], total) ||
validate(set, index + 1, subtotal, total);
Usage:
validate(set, 0, 0, 10);