首页 > 其他分享 >HJ97 记负均正

HJ97 记负均正

时间:2023-07-12 14:33:08浏览次数:29  
标签:String nums int posCnt 记负 num avg HJ97

1. 题目

读题

HJ97 记负均正

 

 

考查点

 

2. 解法

思路

 

代码逻辑

 

具体实现

 


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.valueOf(sc.nextLine());
String[] nums = sc.nextLine().split("\\s+");
count(nums);
}

public static void count(String[] nums) {

int negCnt = 0;
int posCnt = 0;
double posSum = 0;
for (int i = 0; i < nums.length; i++) {
int num = Integer.valueOf(nums[i]);
if (num < 0) {
negCnt++;

} else if (num > 0) {
posCnt++;
posSum += num;
}
}
String avg;
if (posCnt == 0) {
avg = "0.0";
} else {
avg = new DecimalFormat("#0.0").format((double) posSum / posCnt);
}

System.out.print(negCnt + " " + avg);

}
}

3. 总结

标签:String,nums,int,posCnt,记负,num,avg,HJ97
From: https://www.cnblogs.com/shoshana-kong/p/17542219.html

相关文章