tingtingqzh 2023-05-07 21:59 采纳率: 100%
浏览 15
已结题

Wrong Answer


/*题目描述
给定一个等差数列 {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分,求解

img

  • 写回答

1条回答 默认 最新

  • 秉灯 2023-05-08 00:29
    关注

    我不知道你为什么会是90分,但是我建议你把pubdev的声明提前,并且替代if和else中的second-first,因为在if中运算second-first会出现一个临时对象,对性能会有影响

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月10日
  • 已采纳回答 5月10日
  • 创建了问题 5月7日