开发者

Help with a programming challenge

I am trying to make the programming challanges from the book written by Steven S. Skiena and Miguel A. Revilla.

But I am already having problems with the first challenge called "The 3n+1 problem".

The challenge can be found here: http://www.programming-challenges.com/english/pdfs/110101.pdf

The solution I though of is.

#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

int CalcCycleLenght(int n)
{
    int t = 1;
    while (n != 1)
    {
        if (n % 2)
        {
            n = 3 * n + 1;
        }else{
            n = n / 2;
        }
        t++;
    }
    return t;
}

int main(int argc, char* argv[])
{
    int i;
    int j;
    vector<int> vi;
    vector<int> vj;
    vector<int> vm;

    int p = 0;

    while (scanf("%d %d", &i, &j) != EOF)
    {
        vi.push_back(i);
        vj.push_back(j);
        int max = 0;
        int n;
        for (int t = i; t <= j; t++)
        {
            n = CalcCycleLenght(t);
            if (n > max)
                max = n;
        }
        vm.push_back(max);
    }

    for (int i=0; i < vi.size(); i++)
    {
        printf("%d %d %d \n", vi[i], vj[i], vm[i]);
    }   

    return 0;
}

It compiles correctly (although I think I have one extra include) and the answers are correct according to the sample input and output. But the online judge says "wrong answer". I think my problem is the way I input or output the numbers.

[EDIT]

I Solved it. Seems that the problem was that sometimes i was bigger than j. I also removed the vectors and made the code more efficient. Here is the new main function.

int main(int argc, char* argv[])
{
    int i;
    int j;

    int a;
    int b;

    int max;
    int p;

    while (scanf("%d %d", &i, &j) != EOF)
    {
        if (i < j)
        { a = i; b = j; }
        else
        { a = j; b = i; }   

        max = 0;
        for (int t = a; t <= b; t++)
        {           
            p = CalcCycleLenght(t);
            开发者_JS百科if (p > max)
                max = p;
        }
        printf("%d %d %d\n",i,j,max);
    }    
    return 0;
}

Thanks


You output an extra space at the end of each line. Try removing this space, as automated judging often fails on such things. Other than that, your algorithm looks correct to me.

By the way, there's no reason to put the numbers in vectors - you can just output the results for each line as you calculate them.

Edit: It isn't clear from the specifications if it's possible that i>j. Your code will output 0 in that case, so if removing the space doesn't help, you can try swapping i and j in that case so that you have meaningful output (just make sure you output i and j in the same order that you got them).

Edit 2: This is most likely the problem: You use an int in your calculations, which on most platforms can hold values up to 2147483647. But The calculation of the cycle length will require numbers greater than that (for example, for the initial value of 113383). If you use long long (assuming it's supported) you should be OK.


I don't think you're supposed to have a space between the third number and the newline.


The first mistake I noticed was near the end...

printf("%d %d %d \n", vi[i], vj[i], vm[i]);

should be...

printf("%d %d %d\n", vi[i], vj[i], vm[i]);

I didn't see any other mistakes but I didn't read the whole thing that carefully.

One other thing to note is they may not want a new line character (\n) at the end of the last line.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜