简体   繁体   English

最小变更量c ++

[英]Minimum amount of change c++

I need to give the minimum amount of change possible.I input number of cases,each case had a number of coins(1 is not necessarily a part of them) and the number of number I want to test.Then I enter the different coins and the different number to test. 我需要尽可能减少找零的次数。我输入了案件数,每个案件有很多硬币(1不一定是硬币的一部分)和要测试的数目。然后,我输入了不同的硬币以及要测试的号码。

I dont know why my program isnt working.Since 1 isnt necessarily part of the change,I had to tweak the program a little. 我不知道为什么我的程序无法正常工作。由于1不一定是更改的一部分,因此我不得不对程序进行一些调整。

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<functional>
#include<numeric>
#include<algorithm>
#include<vector>

using namespace std;



int main()
{
    int n,i;
    cin>>n;
    int f=n,c,m;
    int flag=0;
    int m1;
    int coins[100];
    vector <int>storage(100,0);
    vector <int> testcases(1000,0);
    vector <int> answers(1000,-1);

    while(n>0)
    {
        cin>>c;
        cin>>m;
        for(i=1;i<=c;i++)
        {
            cin>>coins[i];
        }
        for(i=1;i<=c;i++)
        {
            cin>>testcases[i];
        }
        m1=*max_element(testcases.begin(),testcases.end());
        for(i=0;i<1000;i++)
        {
            answers[i]=-1;
        }


            i=0;
            while(m1>=i)
            {
                i++;
                flag=0;

            for(int j=1;j<=c;j++)
            {
                if(i-coins[j]>=0)
                {
                    storage[j]=answers[i-coins[j]];
                    flag=1;
                }
                else
                storage[j]=-2;

            }
            if(flag==1)
            {answers[i]=*min_element(begin(storage), end(storage),
    [](int t1, int t2) {return t1 > 0 && (t2 <= 0 || t1 < t2);});

            flag=0;
            }
            else
                answers[i]=0;


            }

            if(m1==i)
            {
                for(int y=1;y<=m;y++)
                {
                    cout<<answers[testcases[y]]<<endl;
                }
            }

    }


return 0;
}

EDIT: By "not working" I mean its actually doesnt do anything.Its takes input and does nothing.I think it goes into an infinite loop. 编辑:“不工作”是指它实际上什么也不做。它需要输入,什么也不做。我认为它进入了无限循环。

Universal solution: Run your app under debugger. 通用解决方案:在调试器下运行您的应用程序。 Step into the code, then watch values of variables. 进入代码,然后观察变量的值。 Compare with values that you expect. 与您期望的值进行比较。 Try to edit code, re-compile, and debug again. 尝试编辑代码,重新编译,然后再次调试。 Place a breakpoints in problem places to quickly jump through the code. 在有问题的地方放置一个断点,以快速跳过代码。

I see #include "stdafx.h" , that probably means that you using Visual Studio. 我看到#include "stdafx.h" ,这可能意味着您正在使用Visual Studio。 Here is a guide: 这是一个指南:

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide Visual Studio 2010中的精通调试-入门指南

There could be lots of things wrong with this code (I didn't test it) but one simple problem which would cause an infinite loop is this 这段代码可能有很多问题(我没有测试过),但是一个可能导致无限循环的简单问题是:

while (n > 0)
{
    // lots of code which never changes n
}

You have an infinite loop because nowhere inside the while (n > 0) loop do you modify the value of n . 您有一个无限循环,因为while (n > 0)循环中的任何地方都不会修改n的值。

I would guess you want this 我猜你想要这个

while (n > 0)
{
    // lots of code which never changes n
    --n;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM