問題描述 有 N 個非零且各不相同的整數(shù)。請你編一個程序求出它們中有多少對相反數(shù)(a 和 -a 為一對相反數(shù))。 輸入格式 第一行包含一個正整數(shù) N。(1 ≤ N ≤ 500)。 第二行為 N 個用單個空格隔開的非零整數(shù),每個數(shù)的絕對值不超過1000,保證這些整數(shù)各不相同。 輸出格式 只輸出一個整數(shù),即這 N 個數(shù)中包含多少對相反數(shù)。 樣例輸入 5 1 2 3 -1 -2 樣例輸出 2
第一題都是簡單題,認真就好
#include <iostream>#include <stdio.h>#include<queue>#include<algorithm>#include <cmath>using namespace std;int num[505];int flag[505];int main(){ int n; cin>>n; for(int i=0; i<n; i++) { cin>>num[i]; } int counter=0; for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(flag[j]==0) { if(num[i]==-num[j]) { counter++; flag[i]=flag[j]=1; } } } } cout<<counter<<endl; return 0;}新聞熱點
疑難解答