2 回答
TA贡献2003条经验 获得超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 6
0 1 2 3 4 5
Total: 6
input 6 integers in ascending order: 21 54 68 97 123 412
0 14 26 29 33 43 47 55 69 76 102 289 315 344 358 391
Total: 16
TA贡献1813条经验 获得超2个赞
#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;
}
- 2 回答
- 0 关注
- 707 浏览
添加回答
举报