Description
Suppose that you have an infinite number of containers with different sizes, already filled with water. Given another empty container with size N liters, you need to find the best way to fill this N-liter empty container using the provided smaller containers. By ‘best’, we mean that the total number of used containers should be as small as possible. For example, assume that we have containers in 1, 5, and 10 liters. To get the 17 liters of water, we need 2 containers of 1 liter, 1 container of 5 liters, and 1 container of 10 liters. Another example: assume that we have containers in 1, 5, 10 and 50 liters. To get the 57 liters of water, we need 1 container of 50 liters, 1 container of 5 liters, and 2 containers of 1 liter. Note that, to avoid water wastage, if you choose to use a container, all the water in this container should be poured.
The following is an excerpt of incomplete code:
#include <stdio.h>
#define MAXNV 5
int DONE = 0;
int liters[MAXNV];
int numbers[MAXNV];
void show(int nv);
void filling(int amount, int cur, int nv);
int j=0;
int water;
int min=100;
int min_numbers[MAXNV]={0};
int main(void)
{
int nv, i;
scanf("%d", &nv);
for (i=0; i<nv; i++) {
scanf("%d", &liters[i]);
}
scanf("%d", &water);
filling(water, 0, nv);
for(i=0;i<nv;i++)
numbers[i]=min_numbers[i];
show(nv);
return 0;
}
void show(int nv)
{
int i;
printf("(%d", numbers[0]);
for (i=1; i<nv; i++) {
printf(",%d", numbers[i]);
}
printf(")\n");
}
void filling(int amount, int cur, int nv)
{
int i,j,count_numbers=0;
if(cur<nv){
if(amount==0){
for(i=0;i<nv;i++)
count_numbers=count_numbers+numbers[i];
if(count_numbers<min){
min=count_numbers;
for(i=0;i<nv;i++)
min_numbers[i]=numbers[i];
}
}
else if(amount>=liters[cur]){
/*your code*/
}
else{
/*your code*/
}
}
}
Input
The input contains three lines. The first line contains an integer N (0
Output
The result can be displayed by calling the function show().
Only the best way to fill the empty container should be displayed.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The Tower of Hanoi is a mathematical game puzzle. It consists of three rods, which are A, B and C. The puzzle starts with n disks in ascending order of size on rod A, the smallest at the top.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.
Write a program to simulate the moves of the disks. Print the number of disk which is moved in each step.
For example, if n = 3, the moves of each steps are:
move disk 1 from rod A to rod C
move disk 2 from rod A to rod B
move disk 1 from rod C to rod B
move disk 3 from rod A to rod C
move disk 1 from rod B to rod A
move disk 2 from rod B to rod C
move disk 1 from rod A to rod C
You should print out:
1
2
1
3
1
2
1
The following graph is an easy example when n = 3:

You only need to complete following function:
#include <stdio.h>
#include "function.h"
void hanoi(int n, char A, char B, char C)
{
// Write your code here
}
You may refer to [11656 Simple Permutation] for the submission specifications.
Input
An integer n (0<n<20), which means the number of disk.
Output
Print out the number of disk which is moved in each step, and there is a '\n' at the end of each line.