c++算法:计算差值,得出结果?

c++算法:计算差值,得出结果


蛊毒传说
浏览 708回答 2
2回答

湖上湖

得到的差值个数cin>>R;这是什么意思呢?#include<iostream>#include<iterator>#include<vector>#include<algorithm>using namespace std;int main(){vector<int> v(6,0), diff;cout << "input 6 integers in ascending order: ";for (int i = 0; i < 6; i++) cin >> v[i];for (int i = 0; i < 6; i++)for (int j = 0; j < 6; j++)diff.push_back(abs(v[i] - v[j]));sort(diff.begin(), diff.end());vector<int>::iterator it = unique(diff.begin(), diff.end());diff.resize(it - diff.begin());copy(diff.begin(), diff.end(), ostream_iterator<int>(cout, " "));cout << endl << "Total: " << diff.size() << endl;return 0;}运行一下:input 6 integers in ascending order: 1 2 3 4 5 60 1 2 3 4 5Total: 6input 6 integers in ascending order: 21 54 68 97 123 4120 14 26 29 33 43 47 55 69 76 102 289 315 344 358 391Total: 16

慕姐8265434

#include<iostream>#include<set>using namespace std;int main(){const int N=6;int x[N];set<int> diff_set;for (int i=0;i<N;++i)cin>>x[i];for (int i=1;i<N;++i){for (int j=0;j<i;++j){int diff=x[i]-x[j];cout<<x[i]<<"-"<<x[j]<<"="<<diff<<endl;diff_set.insert(diff);}}cout<<diff_set.size()<<endl;return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP