c++ study

천동민·2023년 4월 25일
0

8,8,8,8,8,
8,2,9,2,8,
3,1,5,1,3,
1,3,5,3,1,
1,1,3,1,1
위의 그래프에서 0~9까지 각 숫자만큼 "="를 출력하고 개수를 출력하라

#include <iostream>


int main()
{

	const int w = 5;
	const int h = 5;
	int cnt[10] = { 0, };
	unsigned char face[w * h] = {
		8,8,8,8,8,
		8,2,9,2,8,
		3,1,5,1,3,
		1,3,5,3,1,
		1,1,3,1,1

	};	
	
		for (int i = 0; i <= 9; i++)
		{
			for (int j = 0; j < 25; j++) {
				if (face[j] == i)
				{
					cnt[i] += 1;
				}
			}

		}
		for (int a = 0; a < 10; a++)
		{
			std::cout << a << ": " << " ";
			for (int j = 0; j < cnt[a]; j++)
			{
				if (cnt[a] != 0)
				{
					std::cout << "= ";
				}
				else
					continue;
			}
			std::cout << cnt[a] << std::endl;
		}
} 

다른 코드

#include <iostream>

int main()
{
	const int w = 5;
	const int h = 5;
	unsigned char face[w * h] = {
		8,8,8,8,8,
		8,2,9,2,8,
		3,1,5,1,3,
		1,3,5,3,1,
		1,1,3,1,1

	};
	const int maxValue = 10;
	int histo[maxValue] = { 0, };
	
	for (size_t i = 0; i < w * h; i++)
	{

		unsigned char f = face[i];//data
		histo[face[i]]++;

	}

	for (size_t i = 0; i < maxValue; i++)
	{
		std::cout << i<<" : ";
		for (size_t h = 0; h < histo[i]; h++)
		{
			std::cout << "=";

		}
		std::cout << histo[i] << std::endl;

	}

	return 1;

}

0개의 댓글