题意
给定一个数组,将数组循环右移M位。
思路
rotate即可。注意m %= n。关于rotate函数的使用,详见cppreference。
代码
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
m %= n;
vector a(n);
对比一下右移运算和除法的效率,整数除法,除以2
对除法和右移做100亿次,计算消耗的时间,为了减少误差的影响,分别做十次,最后取平均值.
结果显示,除法耗时略多点,一百亿次运算多耗时50毫秒左右
除法平均时间:3965
右移平均时间:3910
本测试纯属娱乐,结论不一定可靠。
public class Test {
public static void main(String[] args) {
long num = 10000000000L;
Long[