阿鑫来了  

1.输入两个字符串,从第一个字符串中删除第二个字符串中所有的字符.

   例如输入        they are students.

                          aeiou

   结果                thy r stdnts.

 

#include<iostream>
#include<string>

using namespace std;

int main(){
    //用string定义字符串
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);

    int hash[256] = { 0 };

    for (int i = 0; i < str2.size(); i++){
        hash[str2[i]]++;
    }
    string ret = "";
    for (int i = 0; i < str1.size(); i++){
        if (hash[str1[i]] == 0){
            ret = ret + str1[i];
        }
    }
    cout << ret << endl;
    system("pause");
    return 0;

}


cin可以输入 "they" 或 "are",  但不能输入 "they are",  因为 cin 不能输入包含嵌入空格的字符串,  当它读取到空白字符时,  它将停止读取。

而此getline(cin,str1)函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。

 

2.将一句话的单词进行倒置,  标点不倒置,  比如 i like beijing.  经过函数后变为: beijing. like i

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main(){
    string s;
    getline(cin, s);
    //进行整体逆置---  .gnijieb ekil i
    reverse(s.begin(), s.end());

    auto start = s.begin();

    while (start != s.end()){
        auto end = start;
        while (end != s.end() && *end != ' '){
            end++;
        }

        reverse(start, end);

        if (end != s.end()){
            start = end + 1;
        }
        else{
            start = end;
        }
    }
    cout << s << endl;

    system("pause");
    return 0;

}

reverse函数用于逆置在[begin,end)范围内的元素(包括begin指向的元素,不包括end指向的元素),reverse函数没有返回值

 

 

3.牛牛定义排序子序列为一个数组中一段连续的子序列,  并且这段子序列是非递增或非递减的.  牛牛有一个长度为n的整数数组A,  他现在有一个任务是把数组A分为若干段排序子序列,  牛牛想知道他们最少可以把这个数组分为几段排序子序列.

实例,  牛牛可以把数组A划分为[1,2,3]和[2,2,1]两个排序子序列,  至少需要划分为2个排序子序列,所以输出2.

输入      6

             1 2 3 2 2 1

输出      2

#include<iostream>
#include<vector>

using namespace std;

int main(){
    int n;
    int count = 0;
    cin >> n;
    vector<int>a;
    //n+1防止越界
    a.resize(n+1);
    a[n] = 0;

    for (int i = 0; i < n; i++){
        cin >> a[i];
    }

    int i = 0;
    while (i < n){
        if (a[i] < a[i + 1]){
            while (i < n&&a[i] <= a[i + 1]){
                i++;
            }
            count++;
            i++;
        }
        else if (a[i] = a[i + 1]){
            i++;
        }
        else{
            while (i < n&&a[i] >= a[i + 1]){
                i++;
            }
            count++;
            i++;
        }
    }
    cout << count << endl;
    system("pause");
    return 0;
}

 

posted on 2021-05-10 22:49  阿鑫来了  阅读(357)  评论(0)    收藏  举报