找出与指定值相等的两个数组下标
2019-04-12 本文已影响0人
李药师_hablee
求数组长度的方法是length = sizeof(num) / sizeof(num[0])//假设是数组num[]
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num[6],i,j;//假定为6个元素的数组
int target;
int length = sizeof(num) / sizeof(num[0]);
bool find = 0;
int x1, x2;
for (i = 0; i < length; i++)
{
scanf_s("%d", &num[i]);
}
scanf_s("%d", &target);
for (i = 0; i < length-1; i++)
{
for (j = i + 1; j < length; j++)
{
if (num[i] + num[j] == target)
{
find = 1;
x1 = i;
x2 = j;
break;
}
}
}
if (find == 1)
{
printf("[%d,%d]\n", x1, x2);
}
else
{
printf("not finid!\n");
}
system("pause");
return 0;
}
输出