蓝桥杯——动态数组使用

从键盘读入n个整数,使用动态数组存储所读入的整数,并计算它们的和与平均值分别输出。要求尽可能使用函数实现程序代码。平均值为小数的只保留其整数部分。

样例输入
5
3 4 0 0 2
样例输出
9 1
样例输入
7
3 2 7 5 2 9 1
样例输出
29 4
答案:
  

#include <stdio.h>

#include <string.h>

int main(int argc, const char * argv[]) {

    int n;

    scanf("%d",&n);

    

    //输入n个数

    int array[n];

    int i;

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

        scanf("%d",&array[i]);

    }

    

    //计算这n个数的和并输出

    int total;

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

        total += array[i];

    }

    printf("%d",total);

    printf(" %d",total/n);

    

    return 0;

}

原文地址:https://www.cnblogs.com/123qw/p/4382227.html