/*题目描述
给定一个等差数列 {a} 的首项,第二项,末项,求和。
输入格式
输入三个数,分别为等差数列的首项,第二项和末项,保证第二项和末项不相等。
输出格式
如果该等差数列只有三项,输出a1+a2+a3=s。
超过三项只显示前两项和最后一项,中间用 ... 省略。
等号左侧的加数如果是负数要加()。
样例数据
input1
1 2 3
output1
1+2+3=6
input2
-3 -2 0
output2
(-3)+(-2)+...+0=-6
数据范围
-10^5<=ai<=10^5
3<=等差数列的项数n<=10^4*/
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
int main(){
int first,second,last;
cin>>first>>second>>last;
if (first<0){
cout<<'('<<first<<")+";
if (second<0){
cout<<'('<<second<<")+";
if (last<0){
if (second-first!=last-second) cout<<"...+";
cout<<'('<<last<<")=";
}else{
if (second-first!=last-second) cout<<"...+";
cout<<last<<'=';
}
}else{
cout<<second<<'+';
if (last<0){
if (second-first!=last-second) cout<<"...+";
cout<<'('<<last<<")=";
}else{
if (second-first!=last-second) cout<<"...+";
cout<<last<<'=';
}
}
}else{
cout<<first<<'+';
if (second<0){
cout<<'('<<second<<")+";
if (last<0){
if (second-first!=last-second) cout<<"...+";
cout<<'('<<last<<")=";
}else{
if (second-first!=last-second) cout<<"...+";
cout<<last<<'=';
}
}else{
cout<<second<<'+';
if (last<0){
if (second-first!=last-second) cout<<"...+";
cout<<'('<<last<<")=";
}else{
if (second-first!=last-second) cout<<"...+";
cout<<last<<'=';
}
}
}
if (second-first==last-second){
cout<<first+second+last;
return 0;
}
int pubdev=second-first;
int num=second;
int sum=first+second;
while (num<last){
num+=pubdev;
sum+=num;
}
cout<<sum;
return 0;
}
90分,求解
我不知道你为什么会是90分,但是我建议你把pubdev的声明提前,并且替代if和else中的second-first,因为在if中运算second-first会出现一个临时对象,对性能会有影响