• C语言反转字符串函数reverse()


    The behavior of this function template is equivalent to:

    template <class BidirectionalIterator>
      void reverse (BidirectionalIterator first, BidirectionalIterator last)
    {
      while ((first!=last)&&(first!=--last)) {
        std::iter_swap (first,last);
        ++first;
      }
    }

    Attention:

    Bidirectional iterators to the initial and final positions of the sequence to be reversed. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
    BidirectionalIterator shall point to a type for which swap is properly defined.

    Example:

    // reverse algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::reverse
    #include <vector>       // std::vector
    
    int main () {
      std::vector<int> myvector;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9
    
      std::reverse(myvector.begin(),myvector.end());    // 9 8 7 6 5 4 3 2 1
    
      // print out content:
      std::cout << "myvector contains:";
      for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '
    ';
    
      return 0;
    }

    Output:

    myvector contains: 9 8 7 6 5 4 3 2 1

    实例:PAT乙级

    1008 数组元素循环右移问题 (20 分)

    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A​0​​A​1​​⋯A​N−1​​)变换为(A​N−M​​⋯A​N−1​​A​0​​A​1​​⋯A​N−M−1​​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

    输入格式:

    每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。

    输出格式:

    在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

    输入样例:
    6 2
    1 2 3 4 5 6
    
    输出样例:
    5 6 1 2 3 4
    代码:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main(){
        int n,m;
        int a[1000];
        cin>>n>>m;
        m%=n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        reverse(a,a+n);
        reverse(a+m,a+n);
        reverse(a,a+m);
        for(int i=0;i<n;i++){
            cout<<a[i];
            if(i!=n-1)
            cout<<" ";
        }
        return 0;
    }
    天晴了,起飞吧
  • 相关阅读:
    CentOS7.4 + Ambari 2.6.1.5 + HDP 2.6.4.0 安装部署
    分布式业务的异常解决思路
    RPC簡介
    网络I/O模型--07Netty基础
    网络I/O模型--06异步I/O
    网络I/O模型--05多路复用I/O
    网络I/O模型--04非阻塞模式(解除accept()、 read()方法阻塞)的基础上加入多线程技术
    网络I/O模型--03非阻塞模式(ServerSocket与Socket的超时处理)--解除accept()、 read()方法阻塞
    网络I/O模型--02阻塞模式(多线程)
    Android开发(五)——计时器
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/11155460.html
Copyright © 2020-2023  润新知