你如何在序列中找到数字的位置
How do you find the location of a number in a sequence?
假设我有一个序列:int seq[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
{
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
}
相关文章:
- 如何找到数字的位置并使用它们
- 统一位置和属性位置 - 是相同的数字系列吗?
- 数字的总和在奇数,甚至是字符串的位置
- 根据数字在 c++ 中作为矩阵的位置从文件中读取数字
- 计算正确和放错位置的数字
- 如何将数字0-8变成C 的0-8二进制位置
- 你如何在序列中找到数字的位置
- C 更改数字位置
- 我可以读取向量<string>::迭代器的数字位置吗?
- 将一个数字的一个数字与其当前位置相乘,然后使用递归将其和其他数字相加
- C++如何从文件中读取数字并将其分配到数组中的相应位置
- 构建一个程序,将 10 个整数放入数组中并识别重复的数字并输出它们及其位置编号的位置
- 按每行同一位置出现的 4 个数字对文本文件中的 200 行进行排序
- 我需要从用户输入中找到某些数字的位置
- 带有Overload[]运算符的C++MyInteger类,因此索引在位置i返回数字
- (c++)对数组中的重复项进行计数,然后将被计数的数字移动到另一个数组中的相应位置
- 我需要知道如何在c++中将数字四舍五入到小数点后的某个位置
- 找出数组中最小的数字及其位置
- 交换两个数字而不使用额外的空间,如果两个数字都指向相同的位置,则不起作用
- 用户正在输入10个数字,找到最大的负值及其索引/位置