1012 数字分类 (20 分) Java练习&PTA乙级 printf输出浮点数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiang_6/article/details/89710817

傻傻的直接分类讨论


import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a1 = 0, a2 = 0, a3 = 0, a5 = 0, cnt4 = 0;
		double a4 = 0;
		boolean f1 = false, f2 = false, f3 = false, f4 = false, f5 = false;
		int x = 1;
		int n = in.nextInt();
		for(int i = 0; i < n; ++i) {
			int t = in.nextInt();
			if(t%5 == 0) {
				if(t%2 == 0) {
					f1 = true;	
					a1 += t;
				}
			}
			else if(t%5 == 1) {
				f2 = true;
				a2 += (x * t);
				x = -x;
			}
			else if(t%5 == 2) {
				f3 = true;
				a3++;
			}
			else if(t%5 == 3) {
				f4 = true;
				a4 += t;
				cnt4++;
			}
			else if(t%5 == 4) {
				f5 = true;
				a5 = Math.max(a5, t);
			}
		}
		
		if(f1) {
			System.out.print(a1);
		}
		else {
			System.out.print("N");
		}
		if(f2) {
			System.out.print(" " + a2);
		}
		else {
			System.out.print(" N");
		}
		if(f3) {
			System.out.print(" " + a3);
		}
		else {
			System.out.print(" N");
		}
		if(f4) {
			System.out.printf(" %.1f", a4 / cnt4);
		}
		else {
			System.out.print(" N");
		}
		if(f5) {
			System.out.print(" " + a5);
		}
		else {
			System.out.print(" N");
		}
	}
	
}

猜你喜欢

转载自blog.csdn.net/xiang_6/article/details/89710817