C++ 如何在序列中找到一个数字的位置?
假设我有一个序列: 国际序号[4][4]; 那么,假设seq[1][2]=8; 序列中没有其他值产生8。C++ 如何在序列中找到一个数字的位置?,c++,sequence,C++,Sequence,假设我有一个序列: 国际序号[4][4]; 那么,假设seq[1][2]=8; 序列中没有其他值产生8。 如果我想找到一个序列的值并打印出它是哪一个,例如1,2,并使x=1和y=2,我该怎么做?您搜索的内容。查看每个元素,看看它是否包含所需的值。如果序列中存在可以利用的模式,则可以跳过已知不能包含所搜索值的元素,从而加快搜索速度。这正是我要查找的内容。非常感谢。 int x,j; for (int i = 0; i < 4; i++) // looping through row {
如果我想找到一个序列的值并打印出它是哪一个,例如1,2,并使x=1和y=2,我该怎么做?您搜索的内容。查看每个元素,看看它是否包含所需的值。如果序列中存在可以利用的模式,则可以跳过已知不能包含所搜索值的元素,从而加快搜索速度。这正是我要查找的内容。非常感谢。
int x,j;
for (int i = 0; i < 4; i++) // looping through row
{
for(int j = 0; j < 4; j++) //looping through column
{
if (seq[i][j] == 8) //if value matches
{
x = i; y = j; //set value
i = 4; //set i to 4 to exit outer for loop
break; //exit inner for loop
}
}
}
int numberBeingSearchedFor = *Any Value Here*;
int array[*numRows*][*numColumns*];
int firstOccuranceRow = -1, firstOccuranceColumn = -1;
for(int i = 0; i < numRows; ++i)
{
for(int j = 0; j < numColumns; ++j)
{
if(array[i][j] == numberBeingSearchedFor)
{
firstOccuranceRow = i;
firstOccuranceColumn = j;
i = numRows; //Credit to other answer, I've never seen that :) It's cool
break;
}
}
}
if(firstOccuranceRow == -1 || firstOccuranceColumn == -1)
{
//Item was not in the array
}