C/문법

[C] 백준 4344 - 형변환

Rix 2022. 4. 25. 19:13
// 4344
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int c, std, count;
	float score[1000], sum, avg, percent;

	scanf("%d", &c);
	for (int i = 0; i < c; i++) // c번 반복
	{
		sum = 0;
		count = 0;
		scanf("%d", &std);
		for (int j = 0; j < std; j++)
		{
			scanf("%f", &score[j]);
			sum += score[j];
		}
		avg = sum / std;
		for (int i = 0; i < std; i++)
		{
			if (avg < score[i]) count += 1;
		}
		percent = ((float)count * 100) / std;
		printf("%.3f%%\n", percent);
	}
	return 0;
}
percent = ((float)count * 100) / std; 부분을 처음에
percent = (count * 100) / std; 라고 해서 계속 0.000%가 나와서 당황했다.
count와 std 모두 int형으로 선언해주었으므로 percent가 float형 이므로 count를 float로 형변환했다.