Home > Back-end >  For help, written in c language program
For help, written in c language program

Time:11-12

CodePudding user response:

 # include & lt; Iostream> 
#include

Void FindNearestNumber (STD: : vector & VecNearestPos, const STD: : vector & VecSourse)
{
VecNearestPos. The clear ();

Size_t nSize=vecSourse. The size ();
If (nSize & gt; 1)
{
VecNearestPos={0, 1};
Int nDis=fabs (vecSourse [0] - vecSourse [1]).
For (size_t I=0; I & lt; NSize - 1; I++)
{
For (size_t j=I + 1; J & lt; NSize; J++)
{
If (fabs (vecSourse [I] - vecSourse [j]) & lt; NDis)
{
VecNearestPos [0]=I;
VecNearestPos [1]=j;
NDis=fabs (vecSourse [I] - vecSourse [j]);
}
}
}
}
}

Int main ()
{
STD: : vector VecTest={66, 83, 31, 16, 22, 79};

STD: : vector VecPos;
FindNearestNumber (vecPos vecTest);

If (2==vecPos. The size ()
{
STD: : cout & lt; <"The first" & lt; }
The else
{
STD: : cout & lt; <"Didn't find meet the requirements for" & lt; }
system("pause");
return 0;
}
  • Related