1

here i have written a program on (3n+1) problem.it's also a problem from UVa. I believe it's a known problem.but when i am going to submit it in that online judge community it is sending me a Time Exceeding Error. the time limit is 3 sec. I have done what my little knowledge can do. If anyone can help me with some more advice I would be glad. my code is:

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
    int loop=1;
    unsigned short *cyclelength;
    while(loop=1){
        unsigned int x,y,i,j,num,count=0,p,k,c,max;
        for(;;){
            cout<<"enter two integers. they must not be equal and must be between 1 and 1000000\n";
            while(!(cin>>i>>j)){
                cout<<"please enter a number\n";
                cin.clear();
                cin.ignore(1000,'\n');
            }
            if(i>=1 && i<1000000 && j>=1 && j<1000000  && i!=j){
                break;
            }
            else{
                printf("try the whole process again\n");
            }
        }
        if(i>j){
            x=i;
            y=j;
        }
        else{
            x=j;
            y=i;
        }/*making x always greater than y*/
        cyclelength=(unsigned short *)malloc(1000000 *sizeof(unsigned short));
        if (NULL==cyclelength){
            printf("process aborted");
            return 0;
        }
        else{
            /*solution part for the range of number. and solution for each number  put into cyclelength.*/
            num=y;
            while(num<=x){
                p=1;
                k=num;
                while(k!=1){
                    if(k%2==0)
                        k=k/2;
                    else
                        k=3*k+1;
                    p+=1;
                    }
                cyclelength[count]=p;
                num+=1;
                count+=1;
            }
            c=0;
            max=cyclelength[c];
            for(c=0;c<x-y-1;c+=1){
                if(max<cyclelength[c+1]){
                    max=cyclelength[c+1];
                }
            }
            free(cyclelength);
            cyclelength = NULL;
            cout<<i<<" "<<j<<" "<<max<<'\n';
        }
    }
}
Nasif Imtiaz Ohi
  • 1,563
  • 5
  • 24
  • 45
  • Have you run the program? It certainly looks like you infinitely loop in your outer `while` to me. You never exit. – David Jul 22 '12 at 12:39
  • 5
    `while(loop=1){` this doesn't do what you think it does. – OmnipotentEntity Jul 22 '12 at 12:40
  • While I do know the [Collatz conjecture](http://en.wikipedia.org/wiki/Collatz_conjecture), it is not obvious what your code is supposed to do. – tiwo Jul 22 '12 at 12:40
  • yeah.. i have made the program continuous .. i certainly can add some code to exit.. but the error is "Time Exceeding Error." so then either i have to run the program for only one input and exit or this continuity is not the problem i am looking for! – Nasif Imtiaz Ohi Jul 22 '12 at 12:42
  • It seems like he's doing this: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36 – OmnipotentEntity Jul 22 '12 at 12:44
  • you're right @OmnipotentEntity .. now if you can help me! – Nasif Imtiaz Ohi Jul 22 '12 at 13:12
  • 1
    The most likely problem is that you're mishandling the input in some respect that is causing your program to wait for more input after the autograder is done giving you input. – OmnipotentEntity Jul 22 '12 at 13:22

2 Answers2

3

The problem is that you are not allowing your program to end when online judging engine has finished providing inputs. You need to detect that the judging engine has finished providing inputs and then exit the program.

There is a sample code (in C) on their website here Spoiler Alert: This is actually a solution to 3n+1 problem that kind of explains this. Notice the following condition in Main.

while (scanf("%d %d\n",&m,&n)==2){//perform logic}

This will keep the program running only while there are inputs to be processed.

Amit Mittal
  • 2,646
  • 16
  • 24
0

If your program is running successfully on your pc, but is giving time limit exceeding problem while you are submitting in any online judge community. It is sure that your algorithm is not most efficient. Online line judge communities put the time limit for any program such that an upper average programmer only can design their algorithm. Try to make your programming algorithm best after that try to take it to any online judge community. If you are using any test like >= or <= in your program it takes too much time to run your program.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
  • 1
    While what you've written is true, the OP's problem is clearly related to I/O handling. – bstpierre Sep 21 '12 at 12:25
  • As he has clearly given his whole code you needn't beat around the bush. And more over this question has an already accepted answer. So point specifically to the problem and if possible suggest any corrections. – Rohit Vipin Mathews Oct 04 '12 at 09:32