置换元素和非置换元素

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.
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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

#include <stdio.h>
#include <stdlib.h>
//function to print the array
void print(int* a,int n){printf("printing ........\n");
for(int i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
int* cyclicallyPermute(int* a,int n){int temp=a[0];//store a[0]
for(int i=0;i<n;i++){//for the last element in the modified array
//it will be starting elemnt
if(i==n-1)
a[i]=temp;
//for other element shift left
else
a[i]=a[i+1];
}
return a;
}
int main()
{
int n;
printf("enter array length,n: ");
scanf("%d",&n);
//allocating array dynamically
int* a=(int*)(malloc(sizeof(int)*n));
printf("enter elements: \n");
//taking input
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before permutation\n");
print(a,n);
//function to permute cyclically
//returning base adress of modified array
a=cyclicallyPermute(a,n);
printf("array after permutation\n");
print(a,n);
return 0;
}

Output

输出量

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

翻译自: https://www.includehelp.com/c-programs/cyclically-permute-the-elements-of-an-array.aspx

置换元素和非置换元素

置换元素和非置换元素_循环置换数组元素的C程序相关推荐

  1. [html] 说说你对html中的置换元素和非置换元素的理解

    [html] 说说你对html中的置换元素和非置换元素的理解 置换元素是指:浏览器根据元素的标签和属性,来决定元素的具体显示内容.例如:浏览器根据标签的src属性显示图片.根据标签的type属性决定显 ...

  2. jq循环输出数组并显示在html,jquery怎么循环输出数组元素?

    jquery怎么循环输出数组元素?下面本篇文章给大家介绍一下使用jquery循环输出数组元素的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 用JQuery循环输出数组元素即数 ...

  3. new 数组_编程-遍历数组元素N次/填充数组至指定长度

    需求如下: 有数组 names=["a","b","c"] 需要遍历数组元素n次,比如: n=2时,期待的输出为:a,b n=5时,期待的输 ...

  4. java1到100的数组_用java定义一个数组,长度为3: 1)循环输入数组元素值(元素值在1到100之间)...

    展开全部 public static void main(String[] args) { int[] a = new int[3]; Scanner in = new Scanner(System. ...

  5. 考试成绩最高分、最低分、平均分(c++)_循环、数组、函数、指针

    设计一统计某班级考试成绩中的最高分.最低分和平均分的程序 一.简单一版的成绩管理,循环语句,并未要求保留每位学生的成绩(这就应该需要用数组了). 1.已知人数,for循环: #include < ...

  6. java找出和最接近指定值_如何找到数组元素与特定值最接近的和?

    小编典典 对于这种问题,通常将使用动态编程.但是,本质上可以归结为保留一组可能的总和,然后将输入值一个接一个地添加,如以下代码所示,并且具有相同的渐近运行时间:O(n K),其中n输入数组的大小和K目 ...

  7. vue 获取数组索引_获取某个数组元素 在 数组中的索引

    例如我们有数字: var arr1 =['aa','bb','cc','dd','ee','ff','gg','hh']; var arr2 = [ {id:1,name:'lanyan'}, {id ...

  8. js循环删除数组元素

    逆向循环就可以避免因索引导致删除不干净的问题 const arr = [1, 2, 3, 9, 8, 7, 4, 6, 5] for (let index = arr.length; index &g ...

  9. java声明不可变数组_如何使数组元素在Java中不可变?

    不,您不能使数组的元素不变. 但是java.util.Collections类的unmodifiableList()方法接受List接口的一个对象(实现其类的对象),并返回给定对象的不可修改形式.用户 ...

最新文章

  1. java 混合排序_字母、数字混合方式,按照自然语言排序,java版
  2. Redis Zrevrangebyscore 命令
  3. oracle创建表语句_利用FME去拼接SQL语句并创建表
  4. 职业规划纵向横向_金融行业之职业规划
  5. WiFi攻击中的“核武器”长啥样?来,今天咱们开开眼...
  6. mysql数据库迁徙_mysql数据迁徙详解
  7. 影像科dsa为什么必须买维修保险_了解什么是DSA,看这篇就够了
  8. vue设置列表数据添加
  9. Kotlin Android Studio 环境搭建
  10. 10-angular.identity
  11. nginx部署vue前端,刷新出现404或者500错误的解决方案
  12. linux基本命令示例_Linux ps命令– 20个真实示例
  13. nacos注册中心demo
  14. jupyter notebook 修改默认文件夹
  15. AndroidStudio Screen Capture 按钮点击提示:Unexpected error while obtain screenshot from device:EOF
  16. abaqus2018安装教程win10_win10怎么安装abaqus v6.12_win10系统abaqus v6.12安装详细教程
  17. Windows Neptune的安装
  18. 数据分析 --- 如何收集数据
  19. win中使用labelImg标注图片
  20. offer收割机: 字节跳动、YY、虎牙、BIGO

热门文章

  1. linux php 版本切换,linux更换PHP版本,多个PHP版本切换
  2. 移动端,fixed bottom问题
  3. HTTP Developer's Handbook Part V: Security 读书笔记
  4. Guava之RangeMap
  5. iOS开发之Masonry框架-使用方法须知
  6. Linux下查看某个进程的网络带宽占用情况
  7. Django 和 html
  8. 简单阐述下OC中UIImage三种创建方式~~~
  9. Method Swizzle黑魔法,修改 ios 系统类库方法(转载)
  10. ZooKeeper启动过程2:FastLeaderElection