我是C的新手,我没有习惯使用像'struct'这样的对象。我正在努力提高简单程序的速度:假设输入由两个列表组成:N个元素之一,M个元素中的另一个。我想知道第二个列表的每个元素,如果它出现在第一个列表中,则输出为1,如果不是则返回为0.最后,输出按第二个列表元素输入的顺序显示。
所以我首先尝试用qsort()命令两个列表,然后比较每个列表,但我的程序输出异常结果。例如,如果我将M修改为2,则输出为4个数字!所以这是我的代码:
#include <stdio.h>
#include <stdlib.h>
//this function detects if the element 'input' is appears in the
//first list called 'c'
//The output returns 0 if not, and the '(i+1)th' place 'input'
//appears in 'c'
int search(int input,int a, int N,int c[N]){
int i;
int res=0;
for(i=a;i<N;i++){
if(input==c[N-1-i]){res=i+1;break;}
else{}
}
return res;
}
//Since we sort each list and since we want the output to appear
//in the order each element the second list's elements were input,
//we define a 'Spec' to keep in mind each index of the second list's
//elements
struct Spec{
int val;
int ind;
};
//function qsort() uses to sort the first list 'c'
int compare (int* a, int* b)
{
return ( *a - *b );
}
//function qsort() uses to sort the second list called 'd'
int comp(const void* a, const void* b)
{
const struct Spec *A = (const struct Spec*) a;
const struct Spec *B = (const struct Spec*) b;
return A->val - B->val;
}
int main()
{
int i;
//the size of the first list 'c'
int N;
scanf("%d",&N);
int rank=0;
//declaring and sorting the first list 'c'
int c[N];
for(i = 0; i < N; ++i)
{scanf("%d", &c[i]);}
qsort (c, N, sizeof(int),compare);
//the size of the second list 'd'
int M;
scanf("%d",&M);
//declaring and sorting the second list 'd'
struct Spec* d;
d = (struct Spec*)calloc(M, sizeof(struct Spec));
for(i = 0; i < M; i++)
{scanf("%d", &d[i].val);}
//initialize the index of the input's elements order
for(i = 0; i < M; i++)
{d[i].ind=i;}
qsort (d, N, sizeof(struct Spec), comp);
//the output will be stored in 'f'
int f[M];
//if the following condition is verified, the the output must
//always be 0
if((d[0].val>c[N-1])||(d[M-1].val<c[0])){
for(i=0;i<M;i++){printf("0");printf("\n");}
}
else{
for (i=0;i<M;i++){
//the output is stored in 'f', and the index to respect
//input's order is then 'd[i].ind'
if((d[i].val<c[0])){f[d[i].ind]=0;}
//if the following condition is verified, the the output must always be 0 for the last 'M-i' outputs
else if((d[i].val>c[N-1]))
{
int j;
for(j=i;j<M;j++)
{
f[d[j].ind]=0;
}
break;
}
else{
//if an element 'd[i]' of the second list 'd'
//appears in the first list 'c', then the output
//stored in 'f' will be '1' and the size of the
//matching (betwenn 'c' and 'd') search can be
//truncated from the first 'rank-1' elements
if(search(d[i].val,rank,N,c)>0){
rank=search(d[i].val,rank,N,c)-1;
f[d[i].ind]=1;
}
else{f[d[i].ind]=0;}
}
}
}
//printing the output
for(i=0;i<M;i++){
printf("%d",f[i]);
printf("\n");
}
}
有人可以帮忙吗?
答案 0 :(得分:1)
您的问题 - 如您的额外输出帖子中所述 - 是由打印循环的位置引起的。你应该将它移到最后一个“else”语句中。
首先测试D的所有元素是否在C中所有元素的外部(更大或更小)。如果是,则全部打印零。这很好,但最后再次打印F,这是额外输出的来源。
现在,要处理代码格式......
答案 1 :(得分:0)
要加快代码速度,请将现有的search()函数替换为Binary Search。
另外,请查看代码行if((d[i].val<c[0])){f[d[i].ind]=0;}
,并确认您希望与c[0]
进行比较,而不是c[i]
。
答案 2 :(得分:0)
阅读您提供的说明和源代码,您似乎正在执行以下操作。
首先,您读入一个值列表,这些值是一种常量,如果它们出现在另一个列表中,您想要知道它。这是SearchFor列表。
接下来,您将读入一个值列表,您想知道第二个列表中的第二个列表中有多少以及哪些值也在第一个列表中。这是ValueExist列表。
为了简化搜索过程,您可以对SearchFor值列表进行排序,这样当您在找到匹配项时比较ValueExist列表中的特定项目或者在SearchFor列表中找到当前项目时与ValueExist列表中的当前项比较小于您找到匹配项的ValueExist列表中的当前项,如果它们相等,或者ValueExist列表中的值不在SearchFor列表中,因为当前比较项SearchFor列表中的值小于ValueExist列表中当前项的值。
因此,进行匹配的例程如下所示:
#include <stdio.h>
#include <stdlib.h>
//this function detects if the element 'input' is appears in the
//first list called 'c'
//The output returns 0 if not, and the '(i+1)th' place 'input'
//appears in 'c'
//Since we sort each list and since we want the output to appear
//in the order each element the second list's elements were input,
//we define a 'Spec' to keep in mind each index of the second list's
//elements
struct Spec {
int iValue;
int iIndex;
};
//function qsort() uses to sort the first list 'c'
int compare (void *a, void *b)
{
return ( *(int *)a - *(int *)b );
}
//function qsort() uses to sort the second list called 'd'
int comp(const void* a, const void* b)
{
const struct Spec *A = (const struct Spec*) a;
const struct Spec *B = (const struct Spec*) b;
return (A->iValue - B->iValue);
}
int main()
{
int i, iExist;
//the size of the first list 'c'
int N;
printf ("Enter number of values for SearchFor list\n");
scanf("%d",&N);
int rank=N;
//declaring and sorting the first list 'c'
int SearchFor[N];
for(i = 0; i < N; ++i)
{
printf ("Enter value for index %d\n", i);
scanf("%d", &SearchFor[i]);
}
qsort (SearchFor, N, sizeof(int), compare);
//the size of the second list 'd'
int M;
printf ("\nEnter number of values to search for\n");
scanf("%d",&M);
//declaring and sorting the second list 'd'
struct Spec* ValueExist = (struct Spec*)calloc(M, sizeof(struct Spec));
for (i = 0; i < M; i++) {
// remember the original index for this value
ValueExist[i].iIndex = i;
// get the value for this index
printf ("Enter value for index %d\n", i);
scanf("%d", &ValueExist[i].iValue);
}
qsort (ValueExist, M, sizeof(struct Spec), comp);
for (iExist = 0; iExist < M; iExist++) {
for (i = 0; i < N; i++) {
if (ValueExist[iExist].iValue == SearchFor[i]) {
// value found
printf ("Value of %d at index %d found.\n", ValueExist[iExist].iValue, ValueExist[iExist].iIndex);
break;
} else if (ValueExist[iExist].iValue < SearchFor[i]) {
break;
}
}
}
return 0;
}
答案 3 :(得分:-1)
您的代码无法成功编译,因为“N”不能是变量c [N];