1 Replies - 548 Views - Last Post: 14 November 2015 - 11:41 PM Rate Topic: -----

#1 LadyYugo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-November 15

Trying to solve egg problem

Posted 14 November 2015 - 08:26 PM

Hello, I'm a beginner at programming and I'm trying to solve this problem.

This is what I have gotten so far:

import static java.lang.System.*;
import java.util.*;
import java.io.*;

public class Eggs
{
   public static void main(String[] args) throws Exception
{ 

 Scanner file;
 file = new Scanner(new File("Eggs.txt"));
   {
   int count = file.nextInt();
   for (int i = 0; i < count; i++)  
  {     
   int num = file.nextInt(); 
   out.println(num/2 + 1/2); 
  }
  
  
  }
  
  }
}



And this is the actual problem:

Problem B: The Egg Seller
Input file: Eggs.txt
10
1
2
3
4
5
6
7
8
9
10

Output: System.out
Time Limit: 3 seconds
A puzzle from medieval times goes like this. A farmer stands in the market square selling chickens eggs from his basket. One day a curious thing happens. A series of customers buy eggs from him, leaving his basket empty (that's not the curious part.)Each of these customers bought a number of eggs equal to half of the eggs in the basket at that time, plus half an egg. Without rounding, each customer bought a whole number of eggs.If there were N customers, how many eggs did the farmer begin with? Write a program to solve this puzzle.
Input:A single integer, M, on a line by itself gives the number of test cases to follow. Then M lines follow, each containing a single integer N, (N <20), giving the number of customers in this test case.
Output:For each test case, output a single line giving the initial number of eggs in the farmers basket.
Sample Input:1 2
Sample Output:3

My output comes out as:
0
1
1
2
2
3
3
4
4
5

I know this is a simple problem to solve but I just can't figure out if I have to make/define any new variables. I've been trying to solve this for too much time and I haven't made any progress. I just need someone to guide me or any hints. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to solve egg problem

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5538
  • View blog
  • Posts: 14,541
  • Joined: 18-April 07

Re: Trying to solve egg problem

Posted 14 November 2015 - 11:41 PM

You are going to need a variable to hold the number of eggs after each iteration. That way when you loop you can see how many eggs are in the basket, add 1/2 and then multiply it by 2. This means you have two variables. One to hold the count read in by the file that controls the loop, then one that keeps track of the number of eggs as you go through loop iterations.

Also keep in mind that you will need two loops here. One that loops through the counts in the file and another that will loop through the count and figure out how many eggs it will yield. If necessary, and you guys have covered them, you may want to create a separate function which takes in the number of customers and returns the number of eggs. That way you can just call the function for each line you read in from the file.

:)

This post has been edited by Martyr2: 14 November 2015 - 11:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1