码迷,mamicode.com
首页 > 其他好文 > 详细

1012. 数字分类 (20)

时间:2016-01-06 15:29:12      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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

/**
 * @author jwang1 Success Factors
 */

public class Main {
  public static void main(String[] args) {
    boolean flag1 = false;
    boolean flag2 = false;
    boolean flag3 = false;
    boolean flag4 = false;
    boolean flag5 = false;
    int a1 = 0, a2 = 0, a3 = 0, a5 = 0;
    double a4 = 0;
    int factor = 1;
    int k = 0;
    int t = 0;
    int A = 0;
    int mod = 0;
    Scanner cin = new Scanner(System.in);
    int n = cin.nextInt();
    for (int i = 0; i < n; i++) {
      t = cin.nextInt();
      A = t / 5;
      mod = t % 5;
      switch (mod) {
      case 0:
        if (A % 2 == 0) {
          a1 += t;
          flag1 = true;
        }
        break;
      case 1:
        a2 += factor * t;
        factor = -factor;
        flag2 = true;
        break;
      case 2:
        a3++;
        flag3 = true;
        break;
      case 3:
        a4 += t;
        k++;
        flag4 = true;
        break;
      case 4:
        if (a5 < t) {
          a5 = t;
          flag5 = true;
        }
        break;
      }
    }
    if (flag1)
      System.out.print(a1 + " ");
    else
      System.out.print("N ");
    if (flag2)
      System.out.print(a2 + " ");
    else
      System.out.print("N ");
    if (flag3)
      System.out.print(a3 + " ");
    else
      System.out.print("N ");
    if (flag4)
      System.out.printf("%.1f ", a4 / k);
    else
      System.out.print("N ");
    if (flag5)
      System.out.println(a5);
    else
      System.out.println("N");
  }
}


#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    bool flag1=false;
    bool flag2=false;
    bool flag3=false;
    bool flag4=false;
    bool flag5=false;
    int N;
    int factor=1;
    int t=0;
    int A=0;
    int mod=0;
    int A1=0,A2=0,A3=0,A5=0;
    double A4=0;
    int k=0;
    while(cin>>N) {
        while(N--)
        {
            cin>>t;                   
            A=t/5;
            mod=t%5;
            switch(mod)
            {
            case 0:
                if(A%2==0) {A1+=t;flag1=true;} break;
            case 1:
                A2 += factor*t; factor=-factor;flag2=true;break;
            case 2:
                A3++;flag3=true;break;
            case 3:
                A4+=t;k++;flag4=true;break;
            case 4:
                if(A5<t) A5=t;flag5=true;break;                                       
            }

        }
        if (flag1)
        cout<< A1<<" ";
        else
        cout<<"N"<<" ";
        if (flag2)
            cout<< A2<<" ";
        else
            cout<<"N"<<" ";
        if (flag3)
            cout<< A3<<" ";
        else
            cout<<"N"<<" ";
        if (flag4)
            printf("%.1lf ",A4/k);
        else
            cout<<"N"<<" ";
        if (flag5)
            cout<<A5;
        else
            cout<<"N";
        cout<<endl;
    }
    return 0;
}

1012. 数字分类 (20)

标签:

原文地址:http://www.cnblogs.com/lcchuguo/p/5105505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!