Arrays 在C中搜索两个数组之间的匹配元素 问题:
有两个数组,其中一个数组包含大量的数字(Arrays 在C中搜索两个数组之间的匹配元素 问题:,arrays,c,for-loop,search,Arrays,C,For Loop,Search,有两个数组,其中一个数组包含大量的数字(arr1),这些数字可能有一些重复序列,例如1,2,4可能在此数组中重复多次。还有另一个数组(arr2)将由用户分配 我需要一种方法来搜索用户为arr2提供的确切输入。要使匹配有效,所有元素必须与用户输入的顺序完全相同 例如,如果用户输入1,2,4,3,则程序必须在arr1中按该确切顺序查找1,2,4,3,这意味着arr1中的1,2,3,4不应算作匹配项。下面是到目前为止我所能想到的,但即使两个数组中的第一个元素匹配,这也会返回true for (int
arr1
),这些数字可能有一些重复序列,例如1,2,4可能在此数组中重复多次。还有另一个数组(arr2
)将由用户分配
我需要一种方法来搜索用户为arr2
提供的确切输入。要使匹配有效,所有元素必须与用户输入的顺序完全相同
例如,如果用户输入1,2,4,3,则程序必须在arr1
中按该确切顺序查找1,2,4,3,这意味着arr1中的1,2,3,4不应算作匹配项。下面是到目前为止我所能想到的,但即使两个数组中的第一个元素匹配,这也会返回true
for (int i=0; i<size1; i++)
{
for (int j = 0; j<size2; j++)
{
if(arr1[i] == arr2[j])
{
printf("found a match %d", j);
break;
}
else if (arr1[i] != arr2[j])
{
printf("not match");
break;
}
for(int i=0;i此代码将为在两个列表中相同位置找到的每个字符返回匹配项。
因此,如果要验证整个列表,应设置一个int,以确定两个列表中是否有一个字符不匹配。
如果要显示匹配的列表,只需在第二个If中打印整个列表
int match = 0;
for (int i=0; i<size1; i++)
{
for (int j = 0; j < size2; j++) {
if (arr1[i] != arr2[j]) {
match = 1;
break;
}
}
if(match == 1){
printf("Not a match");
}else if(match == 0){
printf("The lists match");
printf("First character: %c", arr2[0]);
}
match = 0;
}
int匹配=0;
对于(int i=0;i请尝试以下操作:
#include <stdio.h>
int main()
{
int arr1[] = {1, 2, 4, 2, 2, 2, 1, 1, 2, 4, 3, 4, 4, 1, 2, 4, 4, 1, 3, 3};
int size1 = sizeof arr1 / sizeof arr1[0];
int arr2[] = {1, 2, 4};
int size2 = sizeof arr2 / sizeof arr2[0];
int match;
int i, j;
for (i = 0; i < size1 - size2 + 1; i++) {
match = 0;
for (j = 0; j < size2; j++) {
if (arr1[i + j] != arr2[j]) {
match = 1;
break;
}
}
if (! match) {
printf("matched at %d\n", i);
}
}
return 0;
}
如果要将数组分配给匹配索引列表,修改代码将很容易。您的问题类似于在字符串中查找子字符串。但字符将替换为整数
这是一个可以找到匹配项的程序。它有一些调试打印语句来显示它是如何工作的
#include <stdio.h>
int g_Arr2_Changed=0; //----Global boolean if arr2 changes.
int main(int argc, const char *argv[])
{ int arr1[] = {1, 2, 4, 2, 2, 2, 1, 1, 2, 4, 3, 4, 4, 1, 2, 4, 4, 1, 3, 3};
int size1 = (int) (sizeof(arr1)/sizeof(int));
int arr2[] = {1, 2, 4};
int size2 = (int) (sizeof(arr2)/sizeof(int));
int patternStart = -1; //----Start pattern in arr1.
int nPatternMatch = 0; //----Number of ints matched so far.
for (int i=0; i<size1; i++)
{ int checkInt = arr1[i];
//printf("[%i](%i)", i, checkInt);
if (nPatternMatch == 0) //----No integers are matched yet.
{ //printf("<None>");
//----Is checkInt the start of arr2?
if (arr2[nPatternMatch] == checkInt)
{ patternStart=i; nPatternMatch++; //printf("<start>");
}
}
else //----At least one integer has been matched already.
{ //printf("<Some>");
//----Is checkInt a continuation?
if (arr2[nPatternMatch] == checkInt)
{ nPatternMatch++; //----Matched another integer.
//printf("<matched>");
}
else
{ //printf("<NotMatched>");
i = patternStart; //---- for() will add one.
patternStart = - 1;
nPatternMatch = 0; //----Not match. Start at next int.
}
}
//----If match found print and reset for next.
if (nPatternMatch == size2)
{ printf("Pattern found at %i\n", patternStart);
i = patternStart + 1;
patternStart = -1; nPatternMatch = 0;
}
//----Just in case the global variable says arr2 changed.
if (g_Arr2_Changed != 0) //----Start over.
{ //printf("<arr2 changed.>");
i=0; patternStart = -1; nPatternMatch = 0;
size2 = (int) (sizeof(arr2)/sizeof(int));
}
//printf("\n");
}
return 0;
}
#包括
int g_Arr2_Changed=0;//----如果Arr2发生更改,则为全局布尔值。
int main(int argc,const char*argv[]
{int arr1[]={1,2,4,2,2,1,1,2,4,3,4,4,1,2,4,4,1,3};
int size1=(int)(sizeof(arr1)/sizeof(int));
int arr2[]={1,2,4};
int size2=(int)(sizeof(arr2)/sizeof(int));
int patternStart=-1;//----arr1中的开始模式。
int nPatternMatch=0;//----到目前为止匹配的int数。
对于(int i=0;iYou可以随时问我谢谢你的帮助,但我仍然有一些问题,基本上我的问题是arr1将是一个固定数组,但arr2将是动态的,因为用户将分配进来,所以我需要一种方法来比较用户给arr1的元素,并且只有当整个用户字符串与arr1匹配时才会返回一些内容。我提供了如果你能看的话,请在我的原始问题中添加更多的代码。在arr1中,arr2的字符串重复多次,因此我需要我的程序找到所有这些重复的次数,并返回字符串中第一个数字的索引。输出:0、7、13应该是输出。我修改了代码,以单独显示arr2 matc中是否有任何字符串h、 并显示该字符串的第一个字符。我再次尝试了该代码,由于某种原因,它打印不匹配,但arr2被指定为int arr2[]={1,2,4};在arr1中出现3次。此外,我不明白您的程序将如何验证1,2,4在arr1中是否都按顺序出现,因为如果是1,4,4,则不应将其视为匹配列表。最后,我不需要返回数组的第一个字符的方法,而需要返回所有匹配项所在位置的索引的方法如果我们使用arr2是1,2,4,它应该告诉我们,找到它的指数是0,7,13
matched at 0
matched at 7
matched at 13
#include <stdio.h>
int g_Arr2_Changed=0; //----Global boolean if arr2 changes.
int main(int argc, const char *argv[])
{ int arr1[] = {1, 2, 4, 2, 2, 2, 1, 1, 2, 4, 3, 4, 4, 1, 2, 4, 4, 1, 3, 3};
int size1 = (int) (sizeof(arr1)/sizeof(int));
int arr2[] = {1, 2, 4};
int size2 = (int) (sizeof(arr2)/sizeof(int));
int patternStart = -1; //----Start pattern in arr1.
int nPatternMatch = 0; //----Number of ints matched so far.
for (int i=0; i<size1; i++)
{ int checkInt = arr1[i];
//printf("[%i](%i)", i, checkInt);
if (nPatternMatch == 0) //----No integers are matched yet.
{ //printf("<None>");
//----Is checkInt the start of arr2?
if (arr2[nPatternMatch] == checkInt)
{ patternStart=i; nPatternMatch++; //printf("<start>");
}
}
else //----At least one integer has been matched already.
{ //printf("<Some>");
//----Is checkInt a continuation?
if (arr2[nPatternMatch] == checkInt)
{ nPatternMatch++; //----Matched another integer.
//printf("<matched>");
}
else
{ //printf("<NotMatched>");
i = patternStart; //---- for() will add one.
patternStart = - 1;
nPatternMatch = 0; //----Not match. Start at next int.
}
}
//----If match found print and reset for next.
if (nPatternMatch == size2)
{ printf("Pattern found at %i\n", patternStart);
i = patternStart + 1;
patternStart = -1; nPatternMatch = 0;
}
//----Just in case the global variable says arr2 changed.
if (g_Arr2_Changed != 0) //----Start over.
{ //printf("<arr2 changed.>");
i=0; patternStart = -1; nPatternMatch = 0;
size2 = (int) (sizeof(arr2)/sizeof(int));
}
//printf("\n");
}
return 0;
}