日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/1 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 置换元素和非置换元素_循环置换数组元素的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 arraySo it may seem that the entire shifting can be done byFor i =0:n-1A[i] =A[(i+1)%n];End ForBut this will lead to wrong solution since for i=n-1A[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-1If i==n-1A[i]=temp; //actually it means A[n-1]=A[0]ElseA[i]=A[i+1];End IfEnd For3. 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 elsea[i]=a[i+1];}return a; }int main() { int n;printf("enter array length,n: ");scanf("%d",&n);//allocating array dynamicallyint* a=(int*)(malloc(sizeof(int)*n));printf("enter elements: \n");//taking inputfor(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 arraya=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程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。