當前位置:首頁 » 編程語言 » c語言數組元素查找

c語言數組元素查找

發布時間: 2024-06-13 22:43:22

A. c語言實現整型數組中查找指定元素的函數

#include<stdio.h>
int search(int a[], int n, int searchValue) {
int i;
for(i=0; i<n; i++) if(a[i]==searchValue) return i;
return -1;
}
int main() {
int i;
int a[10],find,idx;
for(i=0; i<10; i++) {
printf("Input a[%d]:",i);
scanf("%d",&a[i]);
}
printf("Input searchValue:");
scanf("%d",&find);
idx=search(a,10,find);
if(idx!=-1) printf("pos=%d",idx);
else printf("not found");
}

B. 在c語言中輸入數組兩個數組,查找重復元素並輸出怎麼寫啊

可以一次讀入N個數據。可以考慮以回車結束讀入的一組。
參考如下寫法:
#include "stdio.h"
#define Max 100
int X[Max]={0,},Y[Max]={0,};

int main()
{
int i=0,j=0;
int a,b;
char c=0;

printf("輸入第一個數組(以空格分開,回車結束)");
while((c!='\n'))
scanf("%d%c",X+i++,&c);

c=0;
printf("輸入第二個數組(以空格分開,回車結束)");
while((c!='\n'))
scanf("%d%c",Y+j++,&c);

for(a=0;a<i;a++)
for(b=0;b<j;b++)
if(X[a]==Y[b])
printf("%d \t",X[a]);

return 0;
}

C. 在一個數組中查找一個數,用C語言怎麼寫代碼

#include<卜激stdio.h>

intmain(void)

{

inti,j,k=0,sz[10]={5,75,89,428,576,5986,7543,8524,9805,1057};

printf("請輸入要唯讓查找指弊局的數:");

scanf("%d",&j);

for(i=0;i<10;i++)

if(sz[i]==j)

{

printf("sz[%d]=%d ",i,sz[i]);

k++;

}

if(!k)

printf("數組中沒有您要查找的數。 ");

return0;

}

熱點內容
電腦版陰陽師怎麼單擊配置式神 發布:2025-08-28 01:09:37 瀏覽:698
sql資料庫中 發布:2025-08-28 01:08:43 瀏覽:704
ftp工作原理視頻 發布:2025-08-28 01:07:59 瀏覽:939
pythonstring數組 發布:2025-08-28 00:54:40 瀏覽:900
陌辰學長是怎麼炸我的世界伺服器的 發布:2025-08-28 00:53:11 瀏覽:892
哪裡下載地牢獵手安卓版 發布:2025-08-28 00:16:53 瀏覽:792
我的世界電腦版網易如何進伺服器 發布:2025-08-28 00:06:40 瀏覽:115
pythondictupdate 發布:2025-08-27 23:57:04 瀏覽:586
陰陽師抽ssr腳本 發布:2025-08-27 23:42:58 瀏覽:47
如何升級mac的python版本升級 發布:2025-08-27 23:35:14 瀏覽:296