首页 > 其他 > 详细

9w5:第九周程序填空题1

时间:2019-01-30 01:16:39      阅读:183      评论:0      收藏:0      [点我收藏+]

描述

下面的程序输出结果是:

1 2 6 7 8 9

请填空:

#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main() {
    int a[] = {8,7,8,9,6,2,1};
// 在此处补充你的代码
    ostream_iterator<int> o(cout," ");
    copy( v.begin(),v.end(),o);
    return 0;
}

输入无输出1 2 6 7 8 9样例输入

样例输出

1 2 6 7 8 9

 

Approach #1:

#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main() {
    int a[] = {8,7,8,9,6,2,1};
// 在此处补充你的代码
    int len = sizeof(a)/sizeof(int);
    set<int> v(a, a+len);
    ostream_iterator<int> o(cout," ");
    copy( v.begin(),v.end(),o);
    return 0;
}

  

Analysis:

看到头文件中有set,很自然地就想到了用set来做这道题。刚开始想的是用set<int> v(a, a+7)来初始化set,但是这种方法,感觉太低效,换个数组就不能用了,所以就改成了用sizeof(a)/sizeof(int)来确定数组的大小。

 

9w5:第九周程序填空题1

原文:https://www.cnblogs.com/ruruozhenhao/p/10336373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!