0

それで、コードを書きました。[-100,100] の間の 100 個の乱数の並べ替えられた配列を生成します。次に、result[] に追加して 10 になる数値のすべてのペアを見つけます。

ただし、意図したとおりに機能しません。乱数と並べ替えは問題ないようです。ペアを見つけるだけの部分です。誰かが私がどこで間違ったのか教えてもらえますか? どんな助けでも大歓迎です。

ありがとう

#include <iostream>
#include <cstdlib> 
#include <algorithm>

 using namespace std;


int main()
{

int array[100];
int result[100];
int totalPair;

srand((unsigned)time(0)); 
   for(int i=0; i<100; i++){ 
       array[i] = (rand()%200)+1; 
       array[i]=array[i]-100;
         }

std::sort(array,array+100); 

int i=0;
int j=99;

int totalPairs=0;

while (i<j && i!=j)
{
      if (array[i]+array[j] == 10)
      {
         result[totalPairs*2] = array[i];
         result[totalPairs*2+1] = array[j];
         totalPairs++;
         ++i;
         --j;

      }
      else
      {
         if (array[i]+array[j] > 10) //make sum smaller reduce J
         {
            --j;
         }
         if (array[i]+array[j] < 10) //make sum larger increase I
         {
            ++i;
         }
         if (array[i]+array[j] == 0)
         {
            ++i;  
         }
      }


}

for(int a=0; a<100; ++a){ 
       cout<<array[a]<<endl;

}

for(int a=0; a<100; ++a){ 
       cout<<result[a]<<endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}
4

0 に答える 0