zoukankan      html  css  js  c++  java
  • CSP201403-1:相反数

    引言: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

     

    • 源代码
     1 # include <stdio.h>
     2 # include <stdlib.h>
     3 # include <memory.h>
     4  
     5 int main(void)
     6 {
     7     int n;  //个数
     8     scanf("%d", &n);
     9    
    10     int result = 0;
    11     int *input = (int *)malloc(sizeof(int) * n);
    12     memset(input, 0, sizeof(int)*n);
    13    
    14     for (int i = 0; i < n; i++)
    15     {
    16          scanf("%d", input+i);
    17     }
    18    
    19     for (int i = 0; i < n; i++)
    20     {
    21          for (int j = i+1; j < n; j++)
    22          {
    23              if (input[i] + input[j] == 0)
    24              {
    25                   result += 1;
    26              }
    27          }
    28     }
    29    
    30     printf("%d
    ", result);  
    31     free(input);
    32     return 0;
    33 }

     

  • 相关阅读:
    Sign Distance Field 2
    矩阵相乘优化
    Editor GUI 的 Gamma Correction
    GPUSkinning 5
    GPUSkinning 2
    RenderTextureFormat.ShadowMap
    战争迷雾
    Texture2DArray(2)
    软件渲染器 YwSoftRenderer
    将 Unity5.3 的老项目升级到 Unity 2018.3 遇到的些许问题。
  • 原文地址:https://www.cnblogs.com/husterzxh/p/8407131.html
Copyright © 2011-2022 走看看