PAT-B-1012. 数字分类(Java)

1012. 数字分类 (20)

package com.hym.PAT_B;

import java.util.Scanner;

/**
 * Created by ymhou on 2016/11/21.
 * PAT官网运行超时(100ms运行时间限制),牛客网运行通过
 */
public class PAT_B_1012 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int num[] = new int[N];
        for(int i=0; i<N; i++){
            num[i] = scanner.nextInt();
        }

        int A1=0,A2=0,A3=0,A5=0,S4=0;
        double A4;
        int countA4=0, count=0;
        for(int i=0; i<N; i++){
            int remainder = num[i]%5;
            if(remainder==0 && num[i]%2==0){
                A1 = A1 + num[i];
            }
            if(remainder==1){
                if(count%2==0){
                    A2 = A2 + num[i];
                }else {
                    A2 = A2 - num[i];
                }
                count++;
            }
            if(remainder==2){
                A3++;
            }
            if(remainder==3){
                S4 = S4 + num[i];
                countA4++;
            }
            if(remainder==4){
                if(A5<num[i]){
                    A5=num[i];
                }
            }
        }


        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(A3+" ");
        }
        if(S4==0){
            System.out.print("N ");
        }else {
            A4 = 1.0*S4/countA4;
            Math.round(A4);
            System.out.print(String.format("%.1f",A4)+" ");
        }
        if(A5==0){
            System.out.print("N");
        }else {
            System.out.print(A5);
        }


    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容