Good elements
阿新 • • 發佈:2018-02-04
ali panel cin ase times font cati osi iostream
描述
Given a sequence including N integers, we define the ith element "good" if it equals the sum of three elements strictly before the position i (an element can be used more than once).
Please tell me the number of good elements int this sequence?
輸入
The first line is an integer N (1 <= N <= 5000), and the next line has N integers indicating the elements of the sequence, each element is between -100000 and 100000.
輸出
Output an integer indicating the number of good elements in the sequence.
樣例輸入
2
1 3
5
1 2 3 4 5
樣例輸出
1
3
計算出前面每兩項之和, 用要判斷的那一個數 減去前面的, 如果它們的結果存在的話 (這裏用1標記), 就說明是好數。
#include <iostream> using namespace std; int num[400005]={0}; //存兩數之和是否存在,有負數 ,所以要開大點 int a[5005]; int main() {int n,m,i,j,flag,ans=0; cin>>n; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++){ for(j=1;j<i;j++){ num[a[i-1]+a[j]+200000]=1; } for(j=1;j<i;j++){ if(num[a[i]-a[j]+200000]==1){ ans++;break; } } } cout<<ans<<endl; }
Good elements