[해결] 배열 초기화

Silvester의 이미지

#include <stdio.h>
#include <math.h>
int main()
{
  int n, i, q, c, sz, idx, idx2, t1, t2, elteres, ce = 0;
  int z, j, diff, times=0, l, m,r,o,b;
    scanf("%d",&n); // 총 횟수를 받아옵니다 
  for (z = 0; z < n; z ++) {
    scanf("%d %d",&q , &c); // q = 받아오는 개수 c = 원하는 숫자 
    int set[q]; // 초기화 
    int diff[l]; // 초기화
    l = 0;
    sz = q;
    for (j = 0 ; j < q ; j++)
    {
      scanf("%d",&set[j]); //1렬로 나열된 데이터를 space로 구분해서 받음.
    }
      for(idx = 0; idx < sz; idx++)
      {
        for(idx2 = idx + 1; idx2 < sz; idx2++)
        {
          if( idx < idx2 )
          {
            diff[l] += abs(set[idx] + set[idx2] - c); // 절대값으로 차이를 알아보자. 
            t1 = set[idx];
            t2 = set[idx2];
            l += 1; //무조건 추가.
          }
        }
        m = l;
 
      }
      for (l=0; l<m; l++){
        printf("diffrenece : %d \n", diff[l]); // 메모리 덤프 확인용 (디버깅용) 
      }
 
      for (r=0; r<c; r++)
      {
        for (o=0 ; o<m ; o++) {
            if(diff[o] == r)
                ++times; // 있으면 있는만큼 추가
        }
        if (times > 0)
            break; //가까운 숫자들 집합 횟수를 셌으므로 끝. 
      }
      printf("%d \n" ,times);
 
    }
 
  return 0;
}
 

두수의 합의 집합 중 주어진 수와 가장 가까운 집합을 구하는 과정입니다.
루프하면 배열의 데이터들이 덤프되고 새 데이터를 가져와야하는데 그렇지 못합니다. 왜그렇죠;;
C 뉴비라서 하나도 모르겠습니다.

익명 사용자의 이미지

뉴비시라니깐 말씀드릴게요..

다른사람과 커뮤니케이션하실때 , 용어를 가능하면 명확한걸 써주셔야합니다.

'덤프' 라는 단어는 이럴때 사용하는게 아니에요.. 회사에서 사용하는 공식문서에서는 맨 앞부분에 그 문서에서 사용하는 단어들에 대해서 따로 정의된 내용을 포함하기도하죠.. 그만큼 용어가 중요한데 , 님이 이렇게 단어를 사용하시면 님에게 배우는사람도 그렇고 님 주변사람도 그렇고 대화하기 힘듭니다.

저건 그냥 배열이 printf 문으로 출력이안되요 .. 정도의 질문이면 되는거구요.

그리고 코드에 문제가 많은데요..

1.님의 의도대로 하려면 int *set; set = (int*)malloc(sizeof(int)*q); 이런식으로 해줘야합니다.

그외에도 코드에 이상한부분이 많네요.. C언어 300제 한번 정도 보시고 그때도 해결안되면 다시 질문해주세요.

익명 사용자의 이미지

이런식으로도 쓰는군요.......하하하하...재밌네요;

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.