c++ - 在 SPOJ AP3(AP - 完成系列)中得到错误答案?

标签 c++ optimization

我在 SPOJ 上遇到问题,here

我为它做了数学运算,最后编码了 solution .

我在 ideone 上答对了,但 SPOJ 以“WA”拒绝了我的回答。

#include <iostream>
#include <cmath>
typedef long long LL;

using namespace std;

int main() {

        LL t,x,y,sum,d,n,cd,a,i;

        cin>>t;

    while(t--)
    {
        cin >> x >> y >> sum;
        d = 5*y + 7*x + 2*sum;

        n = (d + sqrt(d*d - 48*sum*(x+y)))/(2*(x+y));

        cd = ( y - x )/(n-6);

        a = x - 2 * cd;

        cout<<n<<endl;
        for(i=0;i<n;i++) {
            cout<< a + cd * i<<" ";
        }
        cout<<endl;
    }
    return 0;
}

Input:

3

3 7 55

8 11 77

9 17 120

Output:

10

1 2 3 4 5 6 7 8 9 10

7

2 5 8 11 14 17 20

8

1 5 9 13 17 21 25 29

我哪里弄错了。我认为问题可能出在精度上,但我无法检查。我使用的数据类型或精度有误还是需要一些 I/O 优化?输入 [ 5 1 25 ] 是否有效?因为它给出的输出为 [ 7 6 5 4 3 2 1 0 -1 -2 ]

我们将不胜感激。

最佳答案

希望我还不算太晚..有一些精度损失就像你必须先在long double中得到你的答案然后将它转换为long使用 llrint() 函数的 long int 类型..直到现在我还没有解决这个问题,因为我的数学计算没有产生正确的结果..我只使用你的代码来检查结果得到了 AC..这是代码

#include <iostream>
#include <cmath>

typedef long long int LL;

using namespace std;

int main() {

        LL t,x,y,sum,i,a,tointeger,newdiff;
        long double n,cd,d;

        cin>>t;

        while(t--) {
            cin >> x >> y >> sum;
            d = 5.0 * y + 7.0 * x + 2.0 * sum;

            n = (d + sqrt(d * d - 48.0 * sum * (x + y))) / (2.0 * (x + y));
            tointeger = llrint(n);

            cd = (y - x) / (tointeger - 6.0);
            newdiff = llrint(cd);
            a = x - 2 * newdiff;
            cout<<tointeger<<endl;

            for(i=0;i<tointeger;i++) {
                cout<< a + newdiff * i<<" ";
            }
            cout<<endl;
    }
    return 0;
}

关于c++ - 在 SPOJ AP3(AP - 完成系列)中得到错误答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24456393/

相关文章:

c++ - 可变参数结构体臃肿,在结构体末尾添加了额外的填充

c++ - 需要帮助在 Ubuntu 中使用 Eclipse 创建 log4cxx appender

optimization - Logger slf4j 使用 {} 格式化而不是字符串连接的优点

optimization - 近似 log10[x^k0 + k1]

ios - 图像优化 (iOS)

python - 使用 Scipy 实现逻辑回归 : Why does this Scipy optimization return all zeros?

c++ header file compiler issue (Sales_item.h) c++ 入门示例

c++ - 二叉树采访: implement follow operation

c++ - 将 Mat 转换为 <vector<vector>> C++

java - 从文件中读取 long[] 的最快方法?