c언어 배열 관련 질문

seopy의 이미지

제가 10진수를 2진수로 변경해서 총 8개를 출력하고(000~111) -> arr이라는 배열 안에 넣었습니다.
그 다음에 000을 기준으로 001부터 111까지 서로 비교하면서 두 개가 얼마나 차이나는지 dmin 이라는 변수에 저장했습니다.
처음에 000과 001을 비교 -> 000과 010을 비교 -> 000과 011을 비교 -> ... -> 000과 111을 비교
예를 들어, 몇 개를 비교해 차이를 구하자면 000과 010은 가운데만 다르므로 dmin에 1이 저장되있고, 000과 011은 2가 저장되어있는 식이죠.
그래서 여기까지는 잘 진행했습니다.
현재까지 진행한 것을 출력하면 dmin이 1 1 2 1 2 2 3 이렇게 나오게 되는데,
여기서 dmin이 2이상인 애들을 보기 위해 1인 애들을 걸러줍니다. 그러면 2 2 2 3 만 남게되죠.

2 2 2 3 => 011 101 110 111

이제 제가 궁금한 것을 질문하자면 이번에는 1인 애들을 걸러주고 나서 제일 위에 있는 수를 기준으로 남은 수와 위와 같은 방식으로 계산하고 싶은데
어떻게 해줘야할지 모르겠습니다. 여기에서는 011을 기준으로 계산하겠죠!
그래서 계산했을 때, dmin에 2 2 1 이 나오면 맞는건데..

#include <stdio.h>
#include <iostream>
#include "stdafx.h"
 
const int M = 3;
const int N = 1;
const int SIZE = M * N; // SIZE = 3 
 
int main()
{
	int i, j;
	int modul[SIZE]; //modul[3]
	int arr[(1 << SIZE)][SIZE]; //arr[8][3]
	int cnt = 0;
	int temp, dmin;
 
	for (i = 0; i < (1 << SIZE); i++)
	{
		for (j = 0; j < SIZE; j++)
		{
			modul[j] = (i >> ((SIZE - 1) - j)) % 2;
			arr[i][j] = modul[j];
			printf("%d ", modul[j]); // 2진수로 변환이 잘 되었는지 테스트
		}
		cnt++;
		printf("\n");
		temp = 0;
		for (j = 0; j < SIZE; j++)
			if (modul[j] == 1)
				temp++;
	}
	printf("---------------[cnt : %d]\n", cnt);
	for (i = 0; i < (1 << SIZE) - 1; i++)
	{
		dmin = 0;
		for (j = 0; j < SIZE; j++)
		{
			if (arr[0][j] != arr[i + 1][j]) // 000을 기준으로 001~111까지 비교
				dmin++;
		}
		printf("%d ", dmin); // dmin 이 잘 나왔는지 테스트
		if (dmin >= 2)
		{
			for (j = 0; j < SIZE; j++)
			{			
				if (arr[3][j] != arr[i + 1][j])
					dmin++;
			}
		}
	}
	printf("\n\n\n");
	return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.