Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
the problem is :Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:

After the cutting each ribbon piece should have length a, b or c.
After the cutting the number of ribbon pieces should be maximum.
Help Polycarpus and find the number of ribbon pieces after the required cutting.

Input
The first line contains four space-separated integers n, a, b and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide.

Output
Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.

and this is its code :
C++
include <stdio.h>
int main()  
{  
    int n,a,b,c,i,j,max=0;  
    scanf("%d %d %d %d",&n,&a,&b,&c);  
  
    for(i=1;i<=n;i++)  
    {     
        for(j=1;i*a+j*b<=n;j++)  
        {  
            if((n-i*a-j*b)%c==0&&(n-i*a-j*b)/c+i+j>max)  
            {  
                max=(n-i*a-j*b)/c+i+j;  
            }  
        }  
    }  
    printf("%d\n",max);  
  
    return 0;  
}


What I have tried:

i checked the code in visual studio and the output is correct , but i can't understand what they did in for loop (i*a+j*b) can anyone help please?
Posted
Updated 23-Mar-16 7:53am
v2
Comments
CHill60 23-Mar-16 9:23am    
Why not ask the person who wrote the code?
Member 12409720 23-Mar-16 9:26am    
i don't know whose the person who solved it , i got the answer from the internet
F-ES Sitecore 23-Mar-16 9:41am    
This has to be at least the third homework question today. We're not here to do your homework for you. What you posted isn't even c#. Discuss the task with your tutors if you're stuck.
Member 12409720 23-Mar-16 10:30am    
it is not a HW , it is just a question I saw it in a website and i just wanna know how they answer it to improve my knowledge , that's it !!!!, if you don't want to help don't comment here Ugh
Patrice T 23-Mar-16 10:52am    
This was at least HomeWork for the author.

Firstly your code is in C++ not C# - try to be accurate when tagging your questions.

The best way to understand what code is doing is to walk through it with the debugger, making note of variable values and following the track the code takes. This article Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] covers using the debugger in Visual Studio. If you are using a different IDE then search for a tutorial for debugging in that environment.

Many members have suggested that this is your homework. If you had cited the original source of the problem - Problem - 189A - Codeforces[^] then you could have avoided that. It would also have been a good idea to state where you found the code.

Codeforce actually provides a clue on how the problem should be solved
Quote:
The problem is to maximize x+y+z subject to ax+by+cz=n. Constraints are low, so simply iterate over two variables (say x and y) and find the third variable (if any) from the second equation. Find the maximum over all feasible solutions.

Other approaches: Use dynamic programming with each state being the remainder of ribbon. Select the next piece to be a, b or c.


Finally, the "Rod cutting" problem is very similar - this article walks through several approaches ... Dynamic Programming - Rod Cutting[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-16 12:00pm    
5ed.
—SA
Patrice T 23-Mar-16 13:54pm    
My 5 too
Member 12409720 23-Mar-16 14:01pm    
that's really help , thank youuuu *_*
Arasappan 24-Mar-16 0:44am    
chilllllllllllll
As Chill60 said in solution 1: run the program on debugger, step by step will help to understand what it does.

The program answer the problem by trying every possible solution. it is a translation of the problem, the formulas comes directly from the problem. Trying to solve by hand may help to understand.

Remark: the program is not efficient because of a mistake in first loop
C++
for(i=1;i*a<=n;i++)

is the correction.
The program will be at its best if you ensure that a > b > c for large values.
 
Share this answer
 
Comments
Member 12409720 23-Mar-16 14:01pm    
thanks for explanation *_*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900