1012. 数字分类 (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”。

输入样例1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例1:
30 11 2 9.7 9
输入样例2:
8 1 2 4 5 6 7 9 16
输出样例2:
N 11 2 N 9
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		long a=System.currentTimeMillis();
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int[]d=new int[n];
		int  A0 = 0, A1 = 0, A2 = 0, A3 = 0, A33 = 0, temp = 0, A4 = 0, j = 1;

		for (int i = 0; i < n; i++) {
			d[i] = input.nextInt();
			
		}
		for (int i = 0; i < n; i++) {
			
			if (d [i]% 5 == 0 && d[i] % 2 == 0) {
				A0 += d[i];
			} else if (d [i]% 5 == 1) {
				
				if (j % 2 == 0) {
					A1 -= d[i];
				} else {
					A1 += d[i];
				}
				j++;
			} else if (d [i]% 5 == 2) {
				A2++;
			} else if (d [i]% 5 == 3) {
				A33++;
				A3 += d[i];
			} else {
				if (d [i]> A4)
					A4 = d[i];
			}
		}
		input.close();
		
		if (A0 == 0) {
			System.out.print("N" + " ");
		} else {
			System.out.print(A0 + " ");
		}
		if (A1 == 0) {
			System.out.print("N" + " ");
		} else {
			System.out.print(A1 + " ");
		}
		if (A2 == 0) {
			System.out.print("N" + " ");
		} else {
			System.out.print(A2 + " ");
		}
		if (A3 == 0) {
			System.out.print("N" + " ");
		} else {

			System.out.print(new DecimalFormat("0.0").format(A3 * 1.0 / A33) + " ");
		}
		if (A4 == 0) {
			System.out.print("N" + "\n");
		} else {
			System.out.print(A4 + "\n");
		}

		long b=System.currentTimeMillis();
		System.out.println(b-a);
	}

}
这题没有提交成功 一再改进都是运行有错或者超时,索性不管了先。
主要是注意Java中小数格式输出!再有就是要会算程序运行时间!其次注意反思为什么这道题的运行时间这么久?会超时!!!

猜你喜欢

转载自blog.csdn.net/Brant985/article/details/80086087