浇注水,在出了问题? [英] Pouring Water, where goes wrong?

查看:109
本文介绍了浇注水,在出了问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成注水从SPOJ。然而,该系统一直给的错误答案的。但我不能找出了问题。

I have finished Pouring Water from SPOJ. However, the system keeps giving wrong answer. But I cannot find out where goes wrong.

请给出提示。测试用例也是AP preciated。

Please give a hint. Test cases are also appreciated.

#include <iostream>

int countFromA (int a, int b, int c);

int main() {
    int t;
    std::cin>>t;
    std::cin.ignore(5, '\n'); // SPOJ error: iostream limit
    while (t--) {
        int a, b, c;
        std::cin>>a>>b>>c;
        if (c == 0) {
            std::cout<<"0"<<std::endl;
        }
        else if (a == c || b == c) {
            std::cout<<"1"<<std::endl;
        }
        else if ((a < c && b < c) || (a == b)) {
            std::cout<<"-1"<<std::endl;
        }
        else {
            int fromA = countFromA (a, b, c);
            int fromB = countFromA (b, a, c);
            int result;
            if (fromA == -1) {
                result = fromB;
            }
            else if (fromB == -1) {
                result = fromA;
            }
            else if (fromA <= fromB) {
                result = fromA;
            }
            else {
                result = fromB;
            }
            std::cout<<result<<std::endl;
        }
    }
}

int countFromA (int a, int b, int c) {
    int times = 0;
    int a_in = 0;
    int b_in = 0;
    // fill a
    a_in = a;
    times++;
    while (true) {
        // a->b & test
        if (a_in > (b - b_in)) {
            a_in = a_in - (b - b_in);
            b_in = b;
            times++;
            if (a_in == c) {
                return times;
            }
        }
        else {
            b_in = b_in + a_in;
            a_in = 0;
            times++;

答:应该在这儿加上测试。我被编码 A&GT的假设; b 和重用的时候,我忘了。

Answer: should add a test here. I was coding in the assumption of a > b, and when reusing it, I forgot.

        }
        // fill a / empty b
        if (b_in == b) {
            b_in = 0;
        }
        else {
            a_in = a;
        }
        times++;
        // finish
        if (a_in == b - b_in) {
            return -1;
        }
    }
}

我的算法首先检查特殊情况下如果 否则如果 否则如果,然后执行主计算其他。对于其他部分,我写了一个函数,从第一个变量倒到第二个变量。

My algorithm first check special cases if else if else if, and then do the main calculation in else. For the else part, I've written a function to pour from first variable to second variable.

推荐答案

下面是一个暗示。由于这是为了您的利益编码练习,我不会给你完整的答案。

Here is a hint. Since this is a coding exercise for your benefit I won't give you the full answer.

您有可能需要在任何顺序相结合,让你的答案4种不同的操作。假设你的号码是 21,3,15 。您需要同时考虑再三倾销 B A ,直到你有 15 在那里,或者填写 A ,保持倒入 B 然后倾倒 b ,直到你有 15 离开。您当前的操作只是尝试操作的单个序列。你需要以某种方式尝试的两个的从头再挑最好的一个。

You have 4 different operations that you might need to combine in any sequence to get your answer. Suppose that your numbers are 21, 3, 15. You need to look at both repeatedly dumping b into a until you have 15 in there, or fill a, keep pouring it into b then dumping b until you have 15 left. Your current operation just tries a single sequence of operations. You need to somehow try both from scratch then pick the best one.

这篇关于浇注水,在出了问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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