描述: | 首先输入要输入的整数个数n,然后输入n个整数。输出为n个整数中负数的个数,和所有正整数的平均值。 |
知识点: | 函数,指针 |
题目来源: | 内部整理 |
练习阶段: | 初级 |
运行时间限制: | 10Sec |
内存限制: | 128MByte |
输入: | 首先输入一个正整数n, |
输出: | 输出负数的个数,和所有正整数的平均值。 注:输出 当平均值为整数直接输出,如果不为整数保留一小数。 |
样例输入: | 5 1 2 3 4 5 |
样例输出: | 0 3 |
import java.util.Scanner;
public class Test8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0,total=0;
int j=-1;
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
//负数计数
if(temp<0){
count++;
}else{
total+=temp;//正整数求和
}
}
if(total%(n-count)==0){
int d = total/(n-count);
System.out.println(count+" "+d);
}else{
double d = total/(n-count+0.0);
System.out.println(count+" "+d);
}
/* System.out.println(count+" "+((total%(n-count)==0)?total/(n-count+0):total/(n-count+0.0)));
*/
}
}