作者avogau ( 假 装)
看板TransCSI
标题Re: [问题] 高雄大学95计概3,4 96计概4,6,8
时间Tue Jun 30 22:30:15 2009
※ 引述《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