我不明白他们为什么放......? [英] Am not understanding why they put...?

查看:90
本文介绍了我不明白他们为什么放......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:Polycarpus有一条带子,长度为n。他希望以满足以下两个条件的方式切割色带:



切割后,每个色带片应具有长度a,b或c。

切割后,色带片的数量应该是最大的。

帮助Polycarpus并找到所需切割后的色带片数量。


输入

第一行包含四个以空格分隔的整数n,a,b和c(1≤n,a,b,c≤4000) - 原始色带的长度相应地,切割后的带状件的可接受长度。数字a,b和c可以重合。



输出

打印一个数字 - 最大可能的带状数量。保证至少存在一个正确的色带切割。



这是它的代码:

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 :

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;  
}





我的尝试:



i检查了visual studio中的代码并且输出是正确的,但是我无法理解他们在for循环中做了什么(i * a + j * b)任何人都可以帮忙吗?



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?

推荐答案

首先你的代码是用C ++而不是C# - 在标记你的问题时尽量准确。



了解最好的方法代码正在做的是使用调试器遍历它,记录变量值并遵循代码所采用的跟踪。本文掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]涵盖了在Visual Studio中使用调试器。如果您使用的是其他IDE,请在该环境中搜索调试教程。



许多成员建议这是您的作业。如果你引用了问题的原始来源 - 问题 - 189A - Codeforces [ ^ ]那么你可以避免这种情况。说出你找到代码的位置也是一个好主意。



Codeforce实际上提供了如何解决问题的线索
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
引用:

问题是最大化x + y + z受ax + by + cz = n。约束条件很低,因此只需迭代两个变量(比如x和y),然后从第二个等式中找到第三个变量(如果有的话)。找出所有可行解决方案的最大值。



其他方法:使用动态编程,每个状态都是功能区的剩余部分。选择下一个作为a,b或c。

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.





最后,棒切割问题非常相似 - 本文将介绍几种方法... 动态编程 - 棒切割 [ ^ ]


作为 Chill60 在解决方案1中说:在调试器上运行程序,一步一步将有助于理解它的作用。



该程序通过尝试所有可能的解决方案来回答问题。它是问题的翻译,公式直接来自问题。尝试手工解决可能有助于理解。



备注:由于第一次循环中的错误,程序效率不高

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
for(i=1;i*a<=n;i++)



是更正。

如果确保 a > b > c 表示大值。


is the correction.
The program will be at its best if you ensure that a > b > c for large values.


这篇关于我不明白他们为什么放......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆