博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Juggling Algorithm (旋转交换)
阅读量:7209 次
发布时间:2019-06-29

本文共 1545 字,大约阅读时间需要 5 分钟。

  hot3.png

where number of sets is equal to GCD of n and d and move the elements within sets.

If GCD is 1 as is for the above example array (n = 7 and d =2), then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.

Here is an example for n =12 and d = 3. GCD is 3 and

Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}a)	Elements are first moved in first set – (See below diagram for this movement)          arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}b)	Then in second set.          arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}c)	Finally in third set.          arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
#include 
#include
int gcd(int a,int b){ if(b==0) return a; else return gcd(b, a%b);}void leftRo(int *array, int step, int len){ int i, j, k, tmp; for (i = 0; i < gcd(step, len); i++) { tmp = array[i]; j = i; while (1) { k = j + step; if (k >= len) { k -= len; } if (k == i) break; array[j] = array[k]; j = k; } array[j] = tmp; }}void printArr(int *arr, int len) { int i; for (i = 0; i < len; i++) { printf("%d ", arr[i]); } printf("\n");}int main(int argc, char *argv[]){ int arr[12] = {1,2,3,4,5,6,7,8,9,10,11,12}; leftRo(arr, 3, 12); printArr(arr, 12); return 0;}

 

转载于:https://my.oschina.net/tsh/blog/1502550

你可能感兴趣的文章
我与前端 | 视野的重要性
查看>>
亚马逊发布用于Amazon Lightsail的托管数据库
查看>>
Uber:大规模系统下如何构建可伸缩的告警生态系统\n
查看>>
Instana发布微服务应用程序样例
查看>>
EOS和MATRIX共识机制对比
查看>>
苹果将iOS应用带入macOS
查看>>
官宣!微软宣布桌面版 Edge将基于Chromium进行开发\n
查看>>
如何学JavaScript
查看>>
挖财架构师:不能从会计角度设计记账App
查看>>
C# 7.3新特性一览
查看>>
.NET Core 2.1 Preview 2带来网络方面的改进
查看>>
又拍云,音视频CDN加速利器
查看>>
Bitbucket Pipelines在Atlassian的Bitbucket云上提供持续交付功能
查看>>
Python数据可视化的10种技能
查看>>
一地鸡毛 OR 绝地反击,2019年区块链发展指南
查看>>
Kafka团队修改KSQL开源许可,怒怼云厂商
查看>>
今夏发布的Terraform 0.12将提供for循环和第一类表达式
查看>>
GitHub使用Electron重写桌面客户端
查看>>
Microsoft发布Azure Data Factory v2可视化工具的公开预览版
查看>>
周下载量过200万的npm包被注入恶意代码,Vue、Node项目恐受影响
查看>>