zoukankan      html  css  js  c++  java
  • 啊哈,翻转

    问题:

    将一个n元一维向量向左旋转i个位置。例如,当n=8且i=3时,向量abcdefgh旋转为defghabc。简单的代码使用一个n元的中间向量在n步内完成该工作。

    解答:

    简述原理:将问题简化成将ab转换成ba。先对a求逆得arb;再对b求逆得arbr;最后整体求逆(arbr)r得到ba。

    python代码如下,忽略细节:

    1 # -*- coding: utf-8 -*-
    2 s = raw_input("请输入字符串:")
    3 here = raw_input("在哪翻转:")
    4 here = int(here)
    5 lens = len(s)
    6 l1 = s[0:here]
    7 l2 = s[here:lens]
    8 result = l1[::-1] + l2[::-1]
    9 print result[::-1]

  • 相关阅读:
    @雅礼集训01/13
    @hdu
    @bzoj
    @hdu
    @bzoj
    @雅礼集训01/10
    @codeforces
    @spoj
    @bzoj
    @bzoj
  • 原文地址:https://www.cnblogs.com/phil-chow/p/5347495.html
Copyright © 2011-2022 走看看