您的位置:首页 > 编程语言 > Java开发

蓝桥杯2014年以前JAVA历年真题及答案整理——数列特征

2014-03-21 23:49 477 查看
问题描述
给出n个数,找出这n个数的最大值,最小值,和。
输入格式
第一行为整数n,表示数的个数。第二行有n个数,为给定的n个数,每个数的绝对值都小于10000。
输出格式
输出三行,每行一个整数。第一行表示这些数中的最大值,第二行表示这些数中的最小值,第三行表示这些数的和。
样例输入
5
1 3 -2 4 5
样例输出
5
-2
3
数据规模与约定:
1 <= n <= 10000。

java实现:
import java.io.*;
public class Main{
public static void main(String args[])throws Exception{
int n;
int sum=0;
String str=null;
String str2=null;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));

str=buf.readLine();
n=Integer.parseInt(str);

str2=buf.readLine();
String a[]=str2.split(" ");
int array2[]=new int
;
for(int i=0;i<n;i++){

array2[i]=Integer.parseInt(a[i]);
sum=sum+array2[i];
}
java.util.Arrays.sort(array2);
System.out.println(array2[n-1]);
System.out.println(array2[0]);
System.out.println(sum);
}

}

C实现:

#include <stdio.h>

int main()
{
int i, n, a[10000];
int sum, the_min, the_max;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
sum = the_min = the_max = a[0];
for (i = 1; i < n; i++)
{
sum += a[i];
if (the_min > a[i]) the_min = a[i];
if (the_max < a[i]) the_max = a[i];
}
printf("%d\n%d\n%d", the_max, the_min, sum);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐