導航:首頁 > 編程語言 > 編程中刪除數組中的數

編程中刪除數組中的數

發布時間:2024-07-08 01:31:17

⑴ c語言排序後刪除數組中重復的數字再排序輸出

#include<stdio.h>

#include<string.h>

#defineSIZE81

voiddelete_repeat(char*str);

voidbubble_sort(char*str);

intmain()

{

charstr[SIZE];

gets(str);

delete_repeat(str);

bubble_sort(str);

puts(str);

return0;

}

//刪除重復字元

voiddelete_repeat(char*str)

{

/**判斷數組內第i個字元是否與前i-1個字元存在重復:

若重復,刪除該字元,i之後的字元下標均減1,後重新判斷新的第i個字元

若不重復,不做處理,繼續遍歷下一個

*/

for(inti=1;str[i]!='';i++){

for(intj=0;j<i;j++)

{

if(str[i]==str[j])

{

for(intk=i;k<strlen(str)-1;k++)

{

str[k]=str[k+1];

}

str[strlen(str)-1]='';

i--;

}

}

}

}

//冒泡排序

voidbubble_sort(char*str)

{

intswap;

chartemp;

intk=strlen(str);

do{

swap=0;

for(inti=0;i<k-1;i++){

if(str[i]>str[i+1])

{

swap=1;

temp=str[i];

str[i]=str[i+1];

str[i+1]=temp;

}

}

k--;

}while(k>0&&swap);

}

(1)編程中刪除數組中的數擴展閱讀

刪除排序數組中的重復項注意事項

不要使用額外的數組空間,必須在原地修改輸入數組並在使用O(1)額外空間的條件下完成,意味著不能通過新建數組存放的方式解決。

可以考慮增加一個游標的方式(命名為:index),遍歷整個數組,當遇到前一個和後一個不相等的時候就給游標自增,這樣的話對於相同的元素可以直接跳過不統計,最後返回」游標的大小+1「(也即不重復的元素的個數)。

⑵ C璇璦緙栫▼錛屽垹闄ゆ暟緇刟涓鍊間負x鐨勭涓涓鍏冪礌銆

榪欎釜棰樼洰鐨勮В棰樻濊礬涓猴細鍥犱負鏁扮粍a宸查潪閫掑噺鏈夊簭錛屾墍浠ヤ竴鑸鍏堝瑰叾榪涜浜屽垎鏌ユ壘錛屼絾瑕佹壘鍒扮涓涓獂鎵鍦ㄧ殑涓嬫爣錛岀劧鍚庡啀瀵瑰叾榪涜屽垹闄ゃ
int BitSearch(int a[], int x, int left, int right) { /*鍦ㄦ暟緇刟鐨勪粠left鍒皉ight鐨勫尯鍩熷唴浜屽垎鏌ユ壘x絎涓嬈″嚭鐜扮殑浣嶇疆錛屽傛灉涓嶅瓨鍦ㄨ繑鍥-1*/
int mid, i;
while(left <= right) {
mid = (left + right) / 2;
if(a[mid] == x) break;
if(a[mid] > x) right = mid - 1;
if(a[mid] < x) left = mid + 1;
}
if(left > right) return -1;
for(i = mid; i >= left && a[i] == x; i--); //瀵繪壘絎涓涓獂鐨勪綅緗
return i + 1;
}
void Romove(int a[], int *length, int index) {
int i;
if(index < 0 || index >= *length) return ; //鍒犻櫎鐨勪綅緗瓚婄晫
for(i = index; i < *length - 1; i++) a[i] = a[i + 1];
*length--;
}

void delSq(int a[], int x, int *length) {
int index = BitSearch(a, x, 0, *length - 1);
Remove(a, length, index);
}

閱讀全文

與編程中刪除數組中的數相關的資料

熱點內容
awss3命令 瀏覽:356
百度店鋪客戶訂單手機加密 瀏覽:500
釘釘班群文件夾怎麼上傳文件 瀏覽:749
人社app怎麼解綁手機 瀏覽:101
caj文件夾打不開 瀏覽:475
什麼app可以將電量變色 瀏覽:692
解放出你的解壓抖音小游戲 瀏覽:345
什麼方式解壓比較好 瀏覽:266
erp是什麼伺服器 瀏覽:185
python中tmp 瀏覽:23
說明wpf加密過程 瀏覽:144
java讀取list 瀏覽:703
iis7gzip壓縮 瀏覽:39
有什麼安卓機打吃雞好 瀏覽:598
三星u盤加密狗 瀏覽:474
php函數的返回值嗎 瀏覽:587
國企穩定程序員 瀏覽:328
編程貓如何使用教程視頻 瀏覽:220
安卓遠端網頁如何打日誌 瀏覽:218
壓縮flash大小 瀏覽:993