"CCF" to find the opposite number

Source: Internet
Author: User

Topic 1 opposite numbers
Time limit: 1 seconds space limit: 256MB
Problem description
There are n non-0 integers that are not identical. Please make up a program to find out how many of them are opposite numbers (A and? A are pairs of opposite numbers).
Input format
The first line consists of a positive integer n. (1≤n≤500).
The second behavior n a non-0 integer separated by a single space, the absolute value of each number does not exceed 1000, guaranteeing that the integers are different.
Output format
Only one integer is output, that is, how many pairs of opposite numbers are included in the N number.
Input sample
5
123-1-2
Output sample

2


① Linked List method

#include <iostream> #include <time.h> #include <stdlib.h>using namespace Std;class node{public:int Data Node* Next; Node () {next = NULL;}}; Class linkedlist{private:node* head; node* last;int len;p ublic:linkedlist () {head = Null;last = Null;len = 0;} ~linkedlist () {node*p=head; Node*q;while (p) {q = P;p=p->next;delete q;}} void Add (int element) {if (head) {last->next=new Node (); last->next->data=element;last = Last->next;len + +;} Else{head = new Node (); head->data = Element;last = Head;len + +;}} void Remove (int i) {node* p=head; node* q=null;if (i==0) {Q=p;head=head->next;delete Q;return;} while (i--) {q=p;p=p->next;} if (p->next) {node* m = P;p=p->next;q->next = P;delete m;} Else{last = Q;last->next=null;delete p;}} void display () {node* p = head;while (p) {cout<<p->data<< "";p =p->next;} Cout<<endl;} node* GetHead () {return head;}; int main () {LinkedList pn;//positive linkedlist nn;//negative srand ((unsigned int) time (0)); int Tn,n;cin>>tn;while (tn--) {n= Rand()%1000 * ((rand ()%2==0) 1:-1);cout<<n<< "";//cin>>n;if (n>0) {pn.add (n);} Else{nn.add (-n);}} cout<<endl; node* pn_head = Pn.gethead (); node* nn_head;int I,amout=0;while (pn_head) {nn_head = Nn.gethead (); i = 0;while (nn_head) {if (nn_head->data==pn_head- >data) {cout<<nn_head->data<< "-" <<pn_head->data<<endl;amout++;nn.remove (i); break;} I++;nn_head=nn_head->next;} Pn_head=pn_head->next;} Cout<<amout<<endl;return 0;}
② Collection Method (reference address: http://zhidao.baidu.com/link?url= govljwhisgnjhcyu39b7uupe7yb95qxtrviqtpctpzzls-umzfjshm0tw2sxhtrd895mj8p7mwopphtqzujpt7pvtevwpyufv_oueezkf7k)

#include <iostream> #include <set>using namespace Std;int main () {int n,n;set<int> s;cin>>n;int i = N;while (i--) {cin>>n;if (n>=0) {S.insert (N);} Else{s.insert (-n);}} cout<< (N-s.size ()) <<endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"CCF" to find the opposite number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.