4479. Gap

新手赛的题,据说是以前4+2的。老了,都没资格参加。

在大牛的指点下,用STL的set解决,嚓,这样编程实现的话简单得多啊

还好以前有看那本C++ primer,不熟的话,用stl也不是那么好写滴!


主要注意:

  1. set不支持随机访问。所以就只能用自增,自减来移动指针了。
  2. find查找不到返回的是“超出末端的迭代器”。
  3. 注意数据的一种“情况"要考虑周全。
  4. 数据类型用int就够了。

先自己写写看吧。


#include
#include

using namespace std;

int n;
int  ans, x;
char str[10];

int main(){
    while ( scanf("%d", &n) != EOF ){
        set s;
        for (int i=0; i<2; ++i){
            scanf("%s %d", str, &x);
            s.insert(x);
        }
        set::iterator curit, preit, posit;
        int  curval, posval, preval;
        
        curit = s.begin();
        posit = curit;
        ++posit;
        
        ans= *posit - *curit;

        for (int i=2; i



你可能感兴趣的:(iterator,编程,c,ACM题解)