一道考验细节的题
最后是?:运算符用错了
代码如下:
#include<stdio.h>
#include<string.h>
typedef struct Grade{
int num;
int inv;
}Grade;
Grade tmp[10];
int n, m, g, interval[10] = {0};
int main(void)
{
scanf("%d%d%d", &n, &m, &g);
int num = 100 / m;
for(int i = 0, grade; i < n; i++)
{
scanf("%d", &grade);
if(grade != 100)
interval[(int)(grade / num)]++;
else
interval[m - 1]++;
}
//任务一
if(g == 0 || g == 1)
{
for(int i = 0; i < m; i++)
tmp[i].num = interval[i], tmp[i].inv = i;
for(int i = 0; i < m - 1; i++)
for(int j = i + 1; j < m; j++)
if(tmp[i].num < tmp[j].num || (tmp[i].num == tmp[j].num && tmp[i].inv > tmp[j].inv))
{
tmp[i].num ^= tmp[j].num ^= tmp[i].num ^= tmp[j].num;
tmp[i].inv ^= tmp[j].inv ^= tmp[i].inv ^= tmp[j].inv;
}//交换
for(int i = 0; i < m; i++)
{
if(tmp[i].num == 0) break;
if(tmp[i].inv == m - 1)
printf("[%2d,%3d]: %d\n", tmp[i].inv * num, (tmp[i].inv + 1) * num, tmp[i].num);
else
printf("[%2d,%3d): %d\n", tmp[i].inv * num, (tmp[i].inv + 1) * num, tmp[i].num);
}
}
if(g == 0) putchar('\n');
//任务二
if(g == 0 || g == 2)
{
int maxn = 0;
for(int i = 0; i < m; i++)
maxn = (maxn > interval[i]) ? maxn : interval[i];
if(maxn > 50)
{
for(int i = 0; i < m; i++)
interval[i] = interval[i] * 50 / maxn;
}
for(int i = 0; i < m; i++)
{
if(i == m - 1)
printf("[%2d,%3d]:", i * num, (i + 1) * num);
else
printf("[%2d,%3d):", i * num, (i + 1) * num);
for(int j = 0; j < interval[i]; j++)
putchar('*');
putchar('\n');
}
}
return 0;
}