csp 201403-1 相反数

 

 注意:

n个非零且不相同,由于不相同,可以利用绝对值

#include<iostream>
#include<cmath>//abs函数
using namespace std;
int main(){
    int n;//元素个数 
    cin>>n;
    
    int* a=new int[1001];//元素取值范围的数组 
    int count=0,temp;//计数, 
    for(int i=0;i<1001;i++){//i<1001不是i<n 
        a[i]=0;    //数组值赋值为0,避免下文if判断出错 
    }
    //巧妙,也可以用普通的方法,遍历数组找和为0的 
    for(int j=0;j<n;j++){
        cin>>temp;
        if(a[abs(temp)])    count++; 
        else  a[abs(temp)]=1;
    }
    cout<<count<<endl;
} 

 

posted @ 2019-09-08 15:02  祺祥  阅读(122)  评论(0编辑  收藏  举报