STL--vector

Description

戰神編碼器有自己的輸入規律,如果你不連續輸入一個數,那麼你之前輸入的數就會消失。

例如:連續輸入數的間隔不能大於3,當你輸入1,2,4時存在3個數,但當你再輸入8時,就只存在8這一個數。

給你一個戰神編碼器,讓你求在輸入所有內容後,還剩多少個數。

Input

輸入包含多個測試案例,輸入的第一行包含兩個整數N和C(1<=N<=100000,1<=C<=1000000000)。
下一行包含N個輸入的數作爲內容。當N和C均爲0的時候結束

Output

對每一個測試案例輸出一個整數,表示還存在多少個數。

Sample Input 1

6 5
1 3 8 14 19 20

Sample Output 1

3

Sample Input 2

6 1
1 3 5 7 9 10

Sample Output 2

2

代碼:

#include<stdio.h>
#include<vector>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
    int a,n,c,i,l;
    vector<int>v;
    vector<int>::const_iterator p;
    while(scanf("%d %d",&n,&c)&&(n!=0||c!=0)){
        for(i=0;i<n;i++){
            scanf("%d",&a);
            if(!v.empty()){
                p=v.end();
                p--;
                l=*p;
                l=abs(l-a);
                if(l>c)
                v.clear();
            }
            v.insert(v.end(),a);
        }
        printf("%d\n",v.size());
    }
    return 0;

}

看到這個題突然想用一下STL,好久沒用了,想複習一下發現找不到了。。。。尷尬

問題一:

p=v.end();

                p--;

改爲  p=v.back();爲什麼報錯,指的是同一個元素呀。

問題2:

v.insert(a); 報錯,但有些容器可以單變量添加,爲了體現vector的隨地插入功能麼emmmmmmmm



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章