我在 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 intAC
..这是代码
#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/