标签:
输入格式:
每一个输入包括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; }
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5105505.html