在SPOJ AP3中答错答案(AP -完成系列)
Getting Wrong Answer in SPOJ AP3 (AP - Complete The Series)?
我遇到了一个关于SPOJ的问题,在这里
我做了数学计算,并最终编码了解决方案。
我在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;
}
输入:3
3 7 55
8 11 77
9 17 120
输出:10
1 2 3 4 5 6 7 8 9 10
72 5 8 11 14 17 20
81 5 9 13 17 21 25 29
我哪里搞错了。我认为问题可能是精度,但我无法检查。我是错误的数据类型或使用的精度或一些i/O优化需要吗?输入[5 1 25]是否有效?因为它给出的输出为[7 6 5 4 3 2 1 0 -1 -2]
希望我不是太迟,有一些precision loss
,像你必须首先在long double
中获取你的答案,然后使用llrint()
函数将其转换为long 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 或 cpp 系列子函数返回到主函数
- 在 Vulkan Qt 中获取队列系列
- QChart在使用QDateTimeAxis时不显示任何系列数据
- Vulkan 的传输队列系列功能和显卡支持:条件检查是否准确?
- 打印系列时循环出现问题
- 有关如何处理 vulkan 队列系列的问题
- Vulkan --- vkAcquireNextImageKHR 在使用某些队列系列时抛出 std::out_of_ra
- 在WIFI_AP_STA中设置AP和STA之间的路由
- 对于非常大的输入,缺少和重复 1..n 的数字数组.使用1..n系列特性的解决方案.溢出问题
- 不同平台的 fstream 系列实现是否不同?
- 统一位置和属性位置 - 是相同的数字系列吗?
- 协议不支持 C++ 地址系列
- Win32 C API:替代损坏的execl*()系列函数?
- C 总和系列阶乘
- 在C 的COUT系列中创建一个空间
- 查找系列中最大的产品
- 无法使用VTK (vtkDICOMImageReader)识别切片ID/系列ID
- 创建可相互转换的模板类系列
- 生成从 1 到 1000 的卡拉奇特系列的程序,其中包括数字和字母
- 在SPOJ AP3中答错答案(AP -完成系列)