--------------------------------------------------------------------------------------------------------------------------------------------------------
时间限制:1秒 空间限制:32768K 代码长度限制 100 KB
--------------------------------------------------------------------------------------------------------------------------------------------------------
题目描述
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
A1 = 能被5整除的数字中所有偶数的和;
A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
A3 = 被5除后余2的数字的个数;
A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
A5 = 被5除后余4的数字中最大数字。
输入描述:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的 正整数。数字间以空格分隔。
输出描述:
对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。 若其中某一类数字不存在,则在相应位置输出“N”。
输入例子:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出例子:
30 11 2 9.7 9
--------------------------------------------------------------------------------------------------------------------------------------------------------
实现思路:
(1).将输入值放到数组中,然后一次循环,分别处理各个值即可;
(2).需要注意的是,A1-A5中,有可能有的值没有,需要提供标记标示下。
--------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int n = scan.nextInt();
int[]numArr = new int[n];
for (int i = 0; i < numArr.length; i++) {
numArr[i] = scan.nextInt();
}
dealNum(numArr);
}
}
public static void dealNum(int[] numArr){
if(numArr == null || numArr.length == 0){
return ;
}
int A1 = 0,A2 = 0,A3 = 0,A5 = 0,tag = 1,A4_sum = 0,A4_numCount = 0;
boolean a1 = false,a2 = false,a3 = false,a4 = false,a5 = false;
double A4 = 0;
for (int i = 0; i < numArr.length; i++) {
if(numArr[i]%5 == 0 && numArr[i]%2 == 0){
A1 = A1 + numArr[i];
a1 = true;
}
if(numArr[i]%5 == 1){
a2 = true;
A2 = A2 + tag*numArr[i];
if(tag == 1){
tag = -1;
}else{
tag = 1;
}
}
if(numArr[i]%5 == 2){
a3 = true;
A3++;
}
if(numArr[i]%5 == 3){
a4 = true;
A4_numCount++;
A4_sum = A4_sum + numArr[i];
}
if(numArr[i]%5 == 4 && numArr[i] > A5){
a5 = true;
A5 = numArr[i];
}
}
A4 = (double)Math.round((A4_sum*1.0/A4_numCount)*10)/10;
System.out.println((a1==false?"N":A1) + " " + (a2==false?"N":A2) + " " + (a3==false?"N":A3) + " " + (a4==false?"N":A4) + " " + (a5==false?"N":A5));
}
}