Re: [問題] 高雄大學95計概3,4 96計概4,6,8

看板TransCSI作者 ( 假 裝)時間16年前 (2009/06/30 22:30), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
※ 引述《katsuoli (katsuo)》之銘言: : 這兩題我自己暴力解XD : 不知道對不對。 : 麻煩各位幫我看一下 : : 3. : : Suppose that a list L of 8 integers 12,5,13,7,9.14,4,8 are given. ^^^^^^^^ : : Please design a computing method (or algorithm) that determines : : if an integer k exists in L . Return the position of k if it is found : : in L and -1 otherwise. State your algorithm in pseudocode or C/C++/Java. : #include<stdio.h> : #include<stdlib.h> : int sequential(int *, int, int); : main() : { : int i,target,index,count; : int A[8]={12,5,13,7,9.14,4,8}; : scanf("%d",&target); : index = sequential(A, count,target); : if(index!=-1 ) : printf("%d(%d)",target,index); : else : printf("-1"); : system("pause"); : } : int sequential(int *A, int count, int target) : { : int i; : for(i=0; i<count; i++) : if(A[i]==target) : return i; : else : return -1; : } : 兩個我沒辦法解決的問題 : 1.在宣告陣列的時候我沒辦法用float,用了的話函數就會出錯....不知道怎麼辦 : 2.在宣告陣列維度的時候我看範例有的用 A[MAX],我不知道為什麼我不行... 題目有說是 int 所以是 9 14 不是9.14 此外 陣列宣告只能用常數 另外 main 不用寫 只要寫int sequential() 這個就好 還有 倒數第二行的 else 很多餘 最後一點 main裡的 count你沒給初始值 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.42.32.188
文章代碼(AID): #1AIY5vv6 (TransCSI)
文章代碼(AID): #1AIY5vv6 (TransCSI)