求数组长度的方法是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;
}
输出
网友评论