博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
置换元素和非置换元素_循环置换数组元素的C程序
阅读量:2532 次
发布时间:2019-05-11

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

置换元素和非置换元素

Problem statement: Write a c program to cyclically permute the element of an array. (In right to left direction). Array should be taken as input from the user.

问题陈述:编写一个c程序来循环置换array的元素 。 (从右到左方向)。 数组应作为用户的输入。

Explanation with example:

举例说明:

Let the user input for the array be: 4 5 6 7 8 10 11 34 56 1

让用户输入该数组为: 4 5 6 7 8 10 11 34 56 1

The cyclic permutation operation on the array results in rotation of the array by one position in right to left direction.

阵列上的循环置换操作导致阵列从右到左方向旋转一个位置。

Thus the array becomes: 5 6 7 8 10 11 34 56 1 4

因此,该数组变为: 5 6 7 8 10 11 34 56 1 4

i.e. the first element becomes the last element & the rest of the elements are shifted by one position.

也就是说,第一个元素变为最后一个元素,其余元素移位一个位置。

Algorithm:

算法:

To shift the (i+1)th element to the left     can be easily done only by:    A[i] =A[i+1]; // A is the input array    So it may seem that the entire shifting can be done by    For i =0:n-1        A[i] =A[(i+1)%n];    End For        But this will lead to wrong solution since for i=n-1    A[n-1]=A[0] which is correct statement but A[0] has been     updated already. This code snippet will result in     A[0] & A[n-1] to be same which is actually wrong.     So what we need to do is to store A[0] ( staring element)     and assign this value to A[n-1]    Thus, a little modification can lead to correct solution:    1.  Set temp to A[0]    2.  For i=0:n-1            If i==n-1                A[i]=temp; //actually it means A[n-1]=A[0]            Else                A[i]=A[i+1];            End If        End For    3.  Print the updated array.

循环置换数组元素的C实现 (C Implementation for Cyclically Permute the Elements of an Array)

#include 
#include
//function to print the arrayvoid print(int* a,int n){
printf("printing ........\n"); for(int i=0;i

Output

输出量

enter array length,n: 10enter elements:4 5 6 7 8 10 11 34 56 1array before permutationprinting ........4 5 6 7 8 10 11 34 56 1array after permutationprinting ........5 6 7 8 10 11 34 56 1 4

翻译自:

置换元素和非置换元素

转载地址:http://vfazd.baihongyu.com/

你可能感兴趣的文章
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>