为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C++
蛊毒传说 2019-02-06 19:11:59
c++算法:计算差值,得出结果
查看完整描述

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



查看完整回答
反对 回复 2019-03-03
?
慕姐8265434

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;
}


查看完整回答
反对 回复 2019-03-03
  • 2 回答
  • 0 关注
  • 707 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信