Question

USE JAVA, PYTHON OR C TO WRITE AN ALGORITHM FOR THIS EXERCISE: . Chocolate bar puzzle...

USE JAVA, PYTHON OR C TO WRITE AN ALGORITHM FOR THIS EXERCISE:

. Chocolate bar puzzle Given an n × m chocolate bar, you need to break it into nm 1 × 1 pieces. You can break a bar only in a straight line, and only one bar can be broken at a time. Design an algorithm that solves the problem with the minimum number of bar breaks. What is this minimum number? Justify your answer by using the properties of a binary tree.

NOTE: I NEED A CODE FOR THIS EXERCISE WRITTEN IN ANY LANGUAGE.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//To cut the n x m chocolate bar into nm 1x1 pieces, we need minimum mn-1 cuts. The code is shown below :

#include <stdio.h>
int main()
{
   int m,n;
   long ans;
   printf("Enter the value of m :");
   scanf("%d",&m);
   printf("Enter the value of n :");
   scanf("%d",&n);
  
   ans = m*n-1;
   printf("Cuts required to break a %dx%d choco bar into 1x1 bars = %ld\n",m,n,ans);
}

#include <stdio.h> int main() { int m, n; long ans; printf(Enter the value of m :); scanf(%d,&m); printf(Enter the value

You can save the file as filename.c which can be compiled using "cc filename.c". You can run it using "./a.out".

Two sample outputs are shown below :

Enter the value of m :10 Enter the value of n :1 Cuts required to break a 10x1 choco bar into 1x1 bars = 9

Enter the value of m:5 Enter the value of n :3 Cuts required to break a 5x3 choco bar into 1x1 bars = 14

How to arrive at this logic ? Note that we can cut one bar at a time. Each cut increases the total number of bars by 1. To get mn pieces we need mn-1 cuts.

Add a comment
Know the answer?
Add Answer to:
USE JAVA, PYTHON OR C TO WRITE AN ALGORITHM FOR THIS EXERCISE: . Chocolate bar puzzle...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given an n*m chocolate bar, you need to break it into n*m 1*1 pieces. You can...

    Given an n*m chocolate bar, you need to break it into n*m 1*1 pieces. You can break a bar only in a straight line, and only one bar can be broken at a time. Design an algorithm that solves the problem with the minimum number of bar breaks. Decrease and Conquer Divide and Conquer Transform and Conquer

  • Use java code please not C++ or Python. Thank you. Write a recursive algorithm that counts...

    Use java code please not C++ or Python. Thank you. Write a recursive algorithm that counts the number of nodes in a linked list. Analyze the runtime of your algorithm

  • In a sensible language ( any of C#/Java/Python) write a program which correctly calculates add, subtract,...

    In a sensible language ( any of C#/Java/Python) write a program which correctly calculates add, subtract, multiply and divide using our ‘minifloat’ binary format using an algorithm you code yourself.  Some details: Your program only needs to work on two ‘numbers’ at a time, read those in from a text file – failure to read those values from a text file will result in a grade of 0.   For each ‘number’ store the sign, exponent and mantissa separately.   You can hard code your...

  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • I need help with my programming assignment. The language used should be java and the algorithm...

    I need help with my programming assignment. The language used should be java and the algorithm should use search trees so that you play against the computer and he chooses the best move. The tree should have all possibilities on the leaves and you could use recursion to so that it populates itself. The game can be a 3*3 board (no need the make it n*n). Please put comments so that I can understand it. Thanks The game of ‘Walls’...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • EMULATE A PROCESS CONTROL BLOCK In this assignment you will use Java, Python or C++ to...

    EMULATE A PROCESS CONTROL BLOCK In this assignment you will use Java, Python or C++ to create a process control block. All objects described aren’t provided, you will create them. You can divide up this code into separate files or put all code in one file. Proper documentation is essential, if there are no comments 5% of the total grade will be deducted. The process control block object PCB should have the following fields: ID: a unique ID for this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT