置换元素和非置换元素_循环置换数组元素的C程序

置换元素和非置换元素

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 <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

置换元素和非置换元素

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380086.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

最新Asp.net源码推荐列表(4月7日)

好久没有在cnblogs给大家发布asp.net源码了&#xff0c;把最近整理的一些发给大家&#xff0c;希望对大家有所帮助&#xff0c;以后争取保持每周发布&#xff01;- WOBIZ第一季1.2版源码 Hits:29 2008-4-7 [结构图] [^][VS2005Access] 电子商务2.0软件是窝窝团队基于对互联网…

远控免杀专题(23)-SharpShooter免杀

转载&#xff1a;https://mp.weixin.qq.com/s/EyvGfWXLbxkHe7liaNFhGg 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/mete…

MySQL 发展史

一.MySQL 标志说明MySQL的海豚标志的名字叫“sakila”&#xff0c;它是由MySQL AB的创始人从用户在“海豚命名”的竞赛中建议的大量的名字表中选出的。获胜的名字是由来自非洲斯威士兰的开源软件开发者Ambrose Twebaze提供。根据Ambrose所说&#xff0c;Sakila来自一种叫SiSwat…

scanf读取字符_在C语言中使用scanf()读取整数时跳过字符

scanf读取字符Let suppose, we want to read time in HH:MM:SS format and store in the variables hours, minutes and seconds, in that case we need to skip columns (:) from the input values. 假设&#xff0c;我们要读取HH&#xff1a;MM&#xff1a;SS格式的时间 &…

An Algorithm Summary of Programming Collective Intelligence (3)

k-Nearest Neighbors kNN(不要问我叫什么)PCI里面用kNN做了一个价格预测模型&#xff0c;还有一个简单的电影喜好预测。简单来说就是要对一个东西做数值预测&#xff0c;就要先有一堆已经有数值的东西&#xff0c;从里面找出和要预测的东西相似的&#xff0c;再通过计算这些相似…

远控免杀专题(24)-CACTUSTORCH免杀

转载&#xff1a;https://mp.weixin.qq.com/s/g0CYvFMsrV7bHIfTnSUJBw 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/mete…

DOM快捷键

DOM所有的命令(CMD) 步骤/方法 cmd命令大全&#xff08;第一部分&#xff09;winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 wscript--------windows脚本宿主设置 write----------写字板 winmsd---------系统…

病毒的手工排除与分析(更新完毕)

作者简介杨京涛    8年以上的IT行业经验&#xff0c;理解企业需求&#xff0c;有企业ERP软件部署规划能力&#xff0c;有综合布线网络规划和管理能力。熟悉软件以及各类硬件&#xff0c;电话程控设备&#xff0c;各类网络设备的管理维护。有编程基础,熟悉VBA、脚本、批处理…

JavaScript中的String substring()方法和示例

JavaScript | 字符串substring()方法 (JavaScript | String substring() Method) The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string. JavaScript中的Str…

Java——方法(练习九九乘法表)

public static void(int,byte…) xxx(int a,int b) 修饰符 返回值类型 方法名(参数类型 参数名1&#xff0c;参数类型 参数名2…)&#xff5b; 方法体语句&#xff1b; return 返回值&#xff1b; &#xff5d; public class fangfa {public static void main(String[] ar…

UVA 10405-Longest Common Subsequence

最长公共子序列&#xff0c;值得注意的是这道题不能用scanf读字符串。 #include<stdio.h>#include<string.h>#define MAXD 1005char s1[MAXD], s2[MAXD];int dp[MAXD][MAXD];int max( int a, int b){return a > b ? a : b;}void solve(){int len1 strlen(s1 …

对初学者的几点建议

http://www.cnblogs.com/thcjp/archive/2007/06/14/783157.html 天轰穿vs2005入门.net2.0系列视频教程推出已经有接近8个月了&#xff0c;这期间我收到非常多的反馈&#xff0c;我只能用非常来形容&#xff0c;呵呵&#xff0c;当然也了解了很多人的心理和学习方法。但是很遗憾…

系统固件升级_固件和操作系统之间的差异

系统固件升级固件 (Firmware) Firmware is somewhere similar to software but it is not a software. Somehow it is a modified form of software. 固件与软件相似&#xff0c;但不是软件。 不知何故&#xff0c;它是软件的修改形式。 Firmware is fixed data or code that …

cobalt strick 4.0 系列教程 (5)--- 获取立足点

https://blog.ateam.qianxin.com/CobaltStrike4.0%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C_%E4%B8%AD%E6%96%87%E7%BF%BB%E8%AF%91.pdf 0x01 客户端 System Profiler [即探针] System Profiler 是一个为客户端攻击提供的侦察工具。这个工具启动一个本地的 web 服务器&#xff0…

[转]sql,N/$/#/@的含义和作用

declare sql nvarchar(4000)set sql Nselect TotalRecordscount(*) from N( sqlFullPopulate N) a EXEC sp_executesql sql,NTotalRecords int output, TotalRecords output 问题&#xff1a;sql 后面有个N, N 起什么作用? 答案&#xff1a; 加上 N 代表存入数据库时…

Java——方法重载(overload)(比较两个数据是否相等)

重载&#xff1a;方法名相同&#xff0c;参数列表不同&#xff0c;与返回值类型无关 重载的分类&#xff1a; 1&#xff0c;参数个数不同 ①&#xff0c;④&#xff0c;⑤&#xff0c;⑥&#xff1b; 2&#xff0c;参数类型不同 ①&#xff0c;②&#xff0c;③、 ⑤&#x…

scala怎么做幂运算_Scala幂(幂)函数示例

scala怎么做幂运算Scala programming language has a huge set of libraries to support different functionalities. Scala编程语言具有大量的库来支持不同的功能。 scala.math.pow() (scala.math.pow()) The pow() function is used for the exponential mathematical opera…

frame--转载

所谓框架便是网页画面分成几个框窗&#xff0c;同时取得多个 URL。只 要 <FRAMESET> <FRAME> 即可&#xff0c;而所有框架标记 要放在一个总起的 html 档&#xff0c;这个档案只记录了该框架 如何划分&#xff0c;不会显示任何资料&#xff0c;所以不必放入 <…

cobalt strick 4.0 系列教程(6)Payload Artifact 和反病毒规避

0x01 哲学 Strategic Cyber 责任有限公司会定期回答有关规避的问题。Cobalt Strike 是否能够绕过 AV 产品&#xff1f;它能绕过哪些 AV 产品&#xff1f;它多久检查一次&#xff1f; Cobalt Strike 默认的 Artifact 可能会被大多数终端安全解决方案拦截。规避不是 Cobalt Str…

【转】企业开发的困境与变局

原文&#xff1a;企业开发的困境与变局 文 / 刘江 算起来&#xff0c;《程序员》已经有几年时间没有大篇幅讨论企业软件开发这个话题了。这其实挺奇怪的。要知道&#xff0c;按类别来分&#xff0c;国内从事企业软件开发的技术人员是最多的&#xff0c;从CSDN和《程序员》联合举…