置換元素和非置換元素_循環置換數組元素的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.pswp.cn/news/380086.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380086.shtml
英文地址,請注明出處:http://en.pswp.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和《程序員》聯合舉…