UVa Online Judge 3n+1 문제.. -_- 왜 제 코드는 거부할까요.

sayhappy의 이미지

제가 작성한 C코드(제출하면 wrong answer이라고 합니다 )

수정하였습니다.__;

#include <stdio.h>
 
int main(void) {
 
  unsigned int i,re,max,t,start,end,small,large;
 
  while( scanf("%d %d", &start, &end) == 2) {
    if(start >= 1 && start <= 1000000 && end >= 1 && end <= 1000000) {
          max = 0;
          if(start > end) { small=end;large=start; }
          else {small=start;large=end;}
 
          for(i=small;i < large; i++) {
 
            t = i;
            re = 0;
 
            while(t != 1) {
              if( t % 2 == 0 )
                t = t >> 1;
              else
                t = 3 * t + 1;
              re++;
            }
            re++;
 
            if( max < re )
              max = re;
 
          }
 
          printf("%d %d %d\n",start,end,max);
      }
  }
  return 0;
}

인터넷에 어떤분이 올리신 C++코드(제출하면 accept 됩니다)

#include <iostream>
using namespace std;
 
int main(void)
{
  unsigned int maxCycle, start, end, cycle, i, inputNumber;
 
  cout << sizeof(int) << endl;
  while(cin >> start >> end)
  {
    if(start >= 1 && start <= 1000000 && end >= 1 && end <= 1000000)
    {
      cout << start << " " << end << " ";
      if(start > end)
      {
        int temp;
        temp = start;
        start = end;
        end = temp;
      }
      maxCycle = 1;
      for(i = start; i <= end; i++)
      {
        inputNumber = i;
        cycle = 1;
        while(inputNumber != 1)
        {
          if(inputNumber % 2 == 0)
          {
            inputNumber /= 2;
          }
          else if(inputNumber % 2 == 1)
          {
            inputNumber = (inputNumber * 3) + 1;
          }
          cycle++;
        }
 
        if(cycle > maxCycle)
        {
          maxCycle = cycle;
        }
      }
      cout << maxCycle << endl;
    }
  }
 
  return 0;
 
}

두 코드에 사용한 알고리즘은 단순한 것으로 서로 같습니다.
몇 가지 입력값을 넣고 테스트를 해봐도 결과도 같았습니다.
혹시 제가 못보고 있는 것이 있나요ㅠ_ㅠ 아니면 검사할 때 제 코드에 문제가 있다고 보는 걸까요..
너무도 답답해서 -_-; 올려봅니다.

'3n+1' 문제는 다음과 같은 내용입니다.

Quote:
3n+1문제라고 해서...어떤 정수 n에서 시작해 n이 짝수면 /2, 홀수면 X 3 + 1 을 하여서 n=1 이 될때까지 반복하는 것인데요, 예를 들어 n = 22 이면 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 이라는 수열이 만들어지는데,
이 때 사이클 길이는 16입니다(수열의 개수)
자, 이제 정수쌍 i, j가 입력되었을 때 i, j 사이의 모든 수(i, j 포함)에 대한 최대 사이클 길이를 구하라가 문제입니다.
[출처] 3n+1 문제 (The 3n+1 Problem / 알고리즘 트레이닝 북 1번 문제)|작성자 우담바라

오호라의 이미지

problemset No 1.

콜라츠의 문제!! NP문제

요즘은 이메일에서 클라이언트로 바꿨던데...ㅋㅋㅋ

져지시스템이 많이 공개가 안되서 자세한 이유는 모르겠지만, splint 같은 걸로 돌려보시고, warning 을 잘 보시는 것이 좋습니다.

          for(i=small;i 1;
              else
                t = 3 * t + 1;
              re++;
            }
            re++;

이 부분이 오타가 아니라면 infinite-loop 이네요.

Hello World.

sayhappy의 이미지

splint로 돌려보니 어디가 잘못되었는지 어디가 문제인지 짐작이 갑니다.;
감사합니다.^^;

xphone의 이미지

갑자기 학생 때 ACM ICPC대회에 나갔던 추억이 떠오르네요.
그 대회에 나갔던 사람들은 저 사이트에 들어가서 문제들을 풀어보는데...
정말 아주 사소한 이유로 틀렸다고 통보를 받지요.

코드가 보이지 않으니 정확한 코드를 다시 올려야 할 것 같습니다.

댓글 달기

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