Thread: Help! always getting wrong answer.

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    Post Help! always getting wrong answer.

    PROBLEM:
    Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n = 1. Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
    Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
    For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

    The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

    For each pair of input integers i and j, output i, j in the same order in which they appeared in the input and then the maximum cycle length for integers between and including i and j. These three numbers should be separated by one space, with all three numbers on one line and with one line of output for each line of input.

    SAMPLE INPUT:
    1 10
    100 200
    201 210
    900 1000
    SAMPLE OUTPUT:
    1 10 20
    100 200 125
    201 210 89
    900 1000 174

    MY CODE:
    Code:
    #include"stdafx.h"
    #include<iostream>
    using namespace std;
    long long cycleLength(const int& n)
    {
    	int num = n;
    	long long length = 0;
    	while (num != 1)
    	{
    		++length;
    		if (num % 2 == 0)
    			num /= 2;
    		else
    			num = num * 3 + 1;
    	}
    	++length;
    	return length;
    }
    int main()
    {
        int i, j;
        while (cin >> i >> j)
        {
            long long maxLength = 0;
            long long temp;
            for (int itr = i; itr <= j; ++itr)
            {
                temp = cycleLength(itr);
                if (maxLength < temp)
                    maxLength = temp;
            }
            cout << i << ' ' << j << ' ' << maxLength << endl;
        }
    }
    Last edited by V8cTor; 07-10-2015 at 12:05 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I see you ditched the insane vector-pushing approach to counting. Good job.
    So what's the problem?
    Is it actually getting wrong answers or is it just taking too long?

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    I found the solution!
    Previously i ignored the fact that while getting inputs for i and j, j could be greater than i.
    Last edited by V8cTor; 07-10-2015 at 01:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program with the wrong answer
    By adhamhamade in forum C Programming
    Replies: 4
    Last Post: 03-24-2014, 09:18 AM
  2. Getting Wrong answer ???
    By Fazot in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2012, 08:45 AM
  3. Always give me wrong answer
    By amroto in forum C Programming
    Replies: 8
    Last Post: 03-29-2012, 09:43 AM
  4. Wrong answer?
    By eViLrAcEr in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2009, 06:04 AM
  5. wrong answer?
    By cman in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 02:17 AM