30分求助
查看原帖
30分求助
864703
judgementbutcher楼主2023/3/27 19:36
#include<iostream>
using namespace std;
#define maxn 101

int main()
{
  int n;
  cin >> n;
  int a[maxn];
  for (int i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  //因为后面的数要往前面找,所以这我们要用到取模操作得到位置
  int thispos, nextpos;
  int count = 0;
  for (int i = 0; i < n; i++)
  {
    //可能这里就已经越界了,因为数组下标是0-n-1
    thispos = (i+1) % n;
    nextpos = (i+2) % n;
    int j, k;
    for (j = thispos; (j+1) % n != i; j = (j+1) % n)
    {
      for (k = nextpos; k % n != i ; k = (k+1) % n)
      {
        if (a[i] == (a[j] + a[k]))
        {
          count ++;
        }
        
      }
      nextpos = (j + 2) % n;
    }
    
  }
  cout << count;
  system("pause");
  return 0;
  
}
2023/3/27 19:36
加载中...