开发者

Another "Why is my Uva 3n+1 solution not being accepted?" question

开发者 https://www.devze.com 2023-03-22 14:18 出处:网络
Why is my solution failing? http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36

Why is my solution failing?

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36

I wrote this under the assumption that stdin was the source of 开发者_如何学Pythonthe data.

I fully expect that it is a problem with my code, but I am lost as to why I get 'Wrong Answer' as the result. (Compiler Choice was ANSI C)

EDIT: modified to allow parameter 1 > param 2 (but now I get "presentation error" whatever that is)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
  unsigned int p1;
  unsigned int start;
  unsigned int end;
  unsigned int current;
  unsigned int n;
  unsigned int p2;
  unsigned int max_cycle = 0;
  unsigned int current_cycle;

  while(scanf("%u %u", &p1, &p2) != EOF){
    max_cycle = 0;
    start = (p1 < p2 ? p1 : p2);
    end = (p1 < p2 ? p2 : p1);
    current = start;
    while(current <= end){
        n = current;
        current_cycle = 0;
        while(n > 1) {
          if(n & 1)
            n = 3*n+1;
          else
            n = n/2;
          current_cycle++;
        }
        current_cycle++;
        if (max_cycle < current_cycle) max_cycle = current_cycle;
        current++;
    }
    fprintf(stdout, "%u %u %u\n", p1, p2, max_cycle );
  }
  return 0;
}


if (start == 0 || end == 0) continue; is unneeded the end condition is where both are zero so it should be if (start == 0 && end == 0) break; Also return 0 and make it int main() (at least in ANSI C I know if you dont return 0 its wrong no matter what). Also you're assuming i < j which is not always the case (as far as I remember). Good luck on solving it.

0

精彩评论

暂无评论...
验证码 换一张
取 消