判断3个数可不可以构成等差序列

灵感来源:https://www.nowcoder.com/practice/7492dceb022a4bbebb990695c107823e?tpId=122&&tqId=33723&rp=1&ru=/ta/exam-wangyi&qru=/ta/exam-wangyi/question-ranking

实现代码(用STL中的set实现的):

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    set<int> s;
    int n=3; 
    while(n--)
    {
        cin>>x;
        s.insert(x);    
    }
    set<int>::iterator it=s.begin();
    if((*s.begin()+*s.rbegin())==2*(*(++it))) cout<<"这3个数构成等差序列\n";
    else cout<<"这3个数不构成等差序列哒\n";
    return 0; 
}

 

运行截图:

 

 

 

方法二:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    if((a+b==2*c)||(b+c==2*a)||(c+a==2*b)) cout<<"这3个数构成等差序列\n";
    else cout<<"这3个数不构成等差序列哒\n";
    return 0;
}

 

posted @ 2020-08-02 11:58  龙龙666666  阅读(263)  评论(1编辑  收藏  举报