笔训day2

选择题

1、输出格式笔训day2_第1张图片

此题与昨天的题类似,有“-”号时是左对齐,%-m.n    m表示宽度,n表示左起取n位数。

2、常量指针和指针常量

笔训day2_第2张图片

//两种都是常量指针
const int *p1;
int const *p2;
//指针常量
int* const p3

 3、字符数组和字符指针

笔训day2_第3张图片

4、函数指针数组

笔训day2_第4张图片

5、位段

笔训day2_第5张图片

6、+1跳过整个数组

笔训day2_第6张图片

编程题

1、排序子序列

笔训day2_第7张图片

笔训day2_第8张图片 

 

#include 
#include 
using namespace std;

int main() {
    int n;
    cin >> n;
    vector a;
    a.resize(n+1);
    a[n] = 0;
    for(int i = 0;i < n;i++)
    {
        scanf("%d",&a[i]);
    }
    int i = 0,count = 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;
    return 0;
}
// 64 位输出请用 printf("%lld")

 2、字符串倒置

笔训day2_第9张图片

笔训day2_第10张图片 

#include 
#include 
#include 
#include 
using namespace std;

int main() {
    string s;
    getline(cin, s);

    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;
}
// 64 位输出请用 printf("%lld")

你可能感兴趣的:(笔训,c++,算法)