1. 程式人生 > >CSP201403-1:相反數

CSP201403-1:相反數

輸入 www. ron 代碼 輸入格式 http 計算機軟件 malloc 正整數

引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中國計算機學會(CCF)發起的"計算機職業資格認證"考試,針對計算機軟件開發、軟件測試、信息管理等領域的專業人士進行能力認證。認證對象是從事或將要從事IT領域專業技術與技術管理人員,以及高校招考研究生的復試對象。

  • 問題描述

  有 N 個非零且各不相同的整數。請你編一個程序求出它們中有多少對相反數(a 和 -a 為一對相反數)。

  • 輸入格式

  第一行包含一個正整數 N。(1 ≤ N ≤ 500)。

  第二行為 N 個用單個空格隔開的非零整數,每個數的絕對值不超過1000,保證這些整數各不相同。

  • 輸出格式

  只輸出一個整數,即這 N 個數中包含多少對相反數。

  • 樣例輸入

5

1 2 3 -1 -2

  • 樣例輸出

2

  • 源代碼

# include <stdio.h>

# include <stdlib.h>

# include <memory.h>

int main(void)

{

int n; //個數

scanf("%d", &n);

int result = 0;

int *input = (int *)malloc(sizeof(int) * n);

memset(input, 0, sizeof(int)*n);

for (int i = 0; i < n; i++)

{

scanf("%d", input+i);

}

for (int i = 0; i < n; i++)

{

for (int j = i+1; j < n; j++)

{

if (input[i] + input[j] == 0)

{

result += 1;

}

}

}

printf("%d\n", result);

free(input);

return 0;

}

CSP201403-1:相反數