我正在解决一个问题 SPOJ AP2
您将获得第 3 项、第 3 项和系列的总和。您需要系列和系列的打印长度。
使用的逻辑->第一项+最后一项=第三项+倒数第三项
sum=n/2(first+last)
我尝试了很多测试用例,我在所有测试用例中都得到了正确的输出 但是在 spoj 上我得到了错误的答案“WA”
#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long int third;
long long int thirdlast;
long long int sum;
cin>>third>>thirdlast>>sum;
long long int n=(2*sum)/(third+thirdlast);
long long int d= (thirdlast-third)/(n-5); //(2*thirdlast-third*n+3*third)/(5-n);
long long int a=(third-2*d);
//cout<<n<<endl;
//cout<<d<<endl;
//int d=0;
for(int i=1;i<=n;i++){
long long int temp=d*(i-1);
// int t=a+d;
cout<<a+temp<<" ";
}
cout<<endl;
}
}
最佳答案
你可能在这一行溢出了:
n=(2*sum)/(third + thirdlast);
您可以通过这种方式避免溢出:
n = 2 * (sum/(third + thirdlast));
但是,由于third + thirdlast
可能不能除sum
,你必须测试它并在必要时添加n++
。
另一种解决方案是对n
unsigned long long
关于c++ - 在 spoj AP2 上得到错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54126838/