杭电oj~~2008

注意定义数组为浮点型,应为输入有浮点数。另外,使用break对输入1特殊处理

题目描述:

数值统计

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 87039    Accepted Submission(s): 42706


Problem Description
统计给定的n个数中,负数、零和正数的个数。
 

Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
 

Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
 

Sample Input
   
   
   
   
6 0 1 2 3 -1 0 5 1 2 3 4 0.5 0
 

Sample Output
   
   
   
   
1 2 3 0 0 5
 
AC代码:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n;
			n = in.nextInt();
			double a[] = new double[1000005];//定义浮点数数组
			int x=0,y=0,z=0;
			if(n==0)
			{
				break;//对数字1特殊处理
			}
			else
			{
				for(int i=0;i<n;i++)
				{
					a[i] = in.nextDouble();
					if(a[i]<0)
					{
						x = x+1;
					}
					if(a[i]==0)
					{
						y = y+1;
					}
					if(a[i]>0)
					{
						z = z+1;
					}
				}
				System.out.println(x+" "+y+" "+z);
			}
		}
	}

}


你可能感兴趣的:(杭电oj~~2008)