牛客网 数字分类 (20)

题目描述

给定一系列正整数,请按要求对数字进行分类,并输出以下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

======================================

第一次code:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) 
 5     {
 6         Scanner input = new Scanner(System.in);
 7         int n = input.nextInt();
 8         int a[] = new int[n];
 9         int b[] ={0,0,0,0,0};
10         StringBuffer list=new StringBuffer();
int cnt = 0; 11 int cnt2 = 0; 12 for (int i = 0; i < n; i++) 13 { 14 a[i] = input.nextInt(); 15 if (a[i] % 10 == 0) 16 { 17 b[0] += a[i]; 18 } 19 if (a[i] % 5 == 1) 20 { 21 b[1] += a[i] * Math.pow(-1, cnt); 22 cnt++; 23 } 24 if (a[i] % 5 == 2) 25 { 26 b[2]++; 27 } 28 if (a[i] % 5 == 3) 29 { 30 b[3]+=a[i]; 31 cnt2++; 32 } 33 if (a[i] % 5 == 4) 34 { 35 if (b[4]<a[i]) 36 { 37 b[4]=a[i]; 38 } 39 } 40 } 41 String string=String.format("%.1f",(float)b[3]/(float)cnt2); 42 for (int i = 0; i < b.length; i++) 43 { 44 if (b[i]==0) 45 { 46 list.append("N "); 47 } 48 else 49 { 50 if (i==3) 51 { 52 list.append(string+" "); 53 } 54 else 55 { 56 list.append(b[i]+" "); 57 } 58 } 59 } 60 System.out.println(list.toString().trim()); 61 } 62 }
原文地址:https://www.cnblogs.com/niithub/p/5840218.html