编写一个程序,输入一个1到300之间的数据n,然后输入n个1到100范围内的数, 统计1到10、11到20等每10个分数段出现的数值个数,并输出相应的星号和个数。 如下图所示,在1到10范围内生成了4个值,而在11到20范围内并没有生成范围为11到20的值。如果输入的数据不在要求范围内则输出相应的错误提示信息。
输入格式:
输一个整数n(1<=n<=300)。
输入n个整数,取值范围在1到100之间。
输出格式:
输出每个数据段的星号和数据出现的次数。
输入样例1:
30
1 2 3 12 13 14 15 22 23 24 34 45 46 47 48 56 57 66 69 70 71 77 78 79 99 90 88 91 100 6
输出样例1: 1 to 10: ****(4)
11 to 20: ****(4)
21 to 30: ***(3)
31 to 40: *(1)
41 to 50: ****(4)
51 to 60: **(2)
61 to 70: ***(3)
71 to 80: ****(4)
81 to 90: **(2)
91 to 100: ***(3)
#include<stdio.h>
int main()
{
int n, num, i, j=0;
int a[10]={0};
scanf("%d", &n);
if (n < 1 || n>300)
printf("You must enter a number in the range 1 to 300");
else
{
for (i = 0; i < n; i++)
{
scanf("%d", &num);
a[(num - 1) / 10]++;
}
for (i = 0; i < 10; i++)
{
printf("%3d to %3d: ", (i * 10) + 1, (i + 1) * 10);
if (a[i] > 0)
{
for (j = 0; j < a[i]; j++)
printf("*");
printf("(%d)", a[i]);
}
printf("\n");
}
}
return 0;
}