100 - The 3n + 1 problem

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

jaredjohnson
New poster
Posts: 11
Joined: Sun Jan 30, 2005 10:21 pm

Restricted Function error #100 3n+1

Post by jaredjohnson » Sun Jan 30, 2005 10:24 pm

I'm just trying to get going on these online programming contest deals. At the programming-challenges.com website I get a restricted function error for the following code. on the Online-judge.uva.es I just get a wrong answer error.

Can anyone help me out here?


#include <iostream>
using namespace std;

int main()
{
int i, j;
long int count, max, num;


while(cin>>i>>j) {

max=0;
for(int k=i; k<j+1; k++) {
count=1;
num=k;
while(num!=1) {
if(num % 2 == 0)
num = num / 2;
else
num = 3 * num + 1;
count++;
}
if (count > max) max = count;
}
cout<<i<<" "<<j<<" "<<max<<endl;
}
}

eleusive
New poster
Posts: 9
Joined: Tue Sep 28, 2004 6:57 am
Location: United States

Post by eleusive » Mon Jan 31, 2005 2:31 am

I'm not sure about the restricted function error, but you are getting WA because the first input is not necessarily less than the second.

jaredjohnson
New poster
Posts: 11
Joined: Sun Jan 30, 2005 10:21 pm

thanks

Post by jaredjohnson » Wed Feb 02, 2005 1:47 am

I fixed it.

User avatar
mohiul alam prince
Experienced poster
Posts: 120
Joined: Sat Nov 01, 2003 6:16 am
Location: Dhaka (EWU)

Post by mohiul alam prince » Wed Feb 02, 2005 3:21 pm

Hi
why are u not solving this problem by using c/c++
this problem is easy for c/c++

MAP

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Why this Compile Error

Post by ImLazy » Thu Feb 03, 2005 5:47 am

I submit this code for problem #100. Not to solve the problem, but just a test.

Code: Select all

#include<stdio.h>
#include<alloc.h>
#include<string.h>
int main()
{
  char *p;
  char temp[1000];
  while(gets(temp)!=NULL)
  {
    p=malloc(strlen(temp)+1);
    strcpy(p,temp);
    puts(p);
    free(p);
  }
  return 0;
}
But get Compile Error. Why???
I stay home. Don't call me out.

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Thu Feb 03, 2005 5:56 am

alloc.h is not part of the standard libraries.

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Thu Feb 03, 2005 10:19 am

So do you mean I can't use alloc.h for any problem? OK,I see.
I stay home. Don't call me out.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Thu Feb 03, 2005 1:46 pm

If I am not mistaken malloc() and the other similar fns are defined in stdlib.h.
Use it.

Regards,
Suman.

carbing
New poster
Posts: 2
Joined: Tue Jan 25, 2005 5:27 pm

100 please

Post by carbing » Sat Feb 05, 2005 2:41 pm

in the "The Input",it says:
The input will consist of a series of pairs of integers i and j...

what's the meaning of "a series of"?how can i know the number of the "pairs of integers i and j"?

is the number unsure?i don't know how to solve this with C++ if it is unsure.please help me,thank you.

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard » Sat Feb 05, 2005 3:51 pm

Hello carping.
If problem doesn't tell you when does input ends you must read while not end of file.
For C++ you can read for example in such way if you use iostream.

Code: Select all

While (cin >> a >> b)
{
// your solution //
}
or

Code: Select all

While (true) {
cin >> a >> b;
// Your solution //
}
Eduard
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

carbing
New poster
Posts: 2
Joined: Tue Jan 25, 2005 5:27 pm

Post by carbing » Sat Feb 05, 2005 4:00 pm

Thank you for your useful help.I think i have to learn more about the C++ language.Thanks again.

jonaskoelker
New poster
Posts: 7
Joined: Fri Jan 28, 2005 10:01 pm
Location: Denmark
Contact:

time out.

Post by jonaskoelker » Sun Feb 06, 2005 4:14 am

try running this:
time echo "1 1000000" | java Main
that is, take time on how long it takes to solve the biggest problem instance possible. I think you'll be surprised.

To speed it up, you might want to save some of your previous results, so you don't run through a gazillion iterations for each number. Btw, If ints don't overflow: use them; I think it's a safe bet to say that they're gonna be somewhat faster than longs -- assuming that the judges use a 32-bit machine (does kernel 2.4.x exist for 64-bitters?).

Anyways, best of luck,

Jonas.

Insidetheasylum
New poster
Posts: 2
Joined: Wed Feb 09, 2005 5:24 pm

Problem: 100 compile error O.o;

Post by Insidetheasylum » Wed Feb 09, 2005 5:25 pm

Well, I compiled the program on my computer using Dev-C++ (4.9.9.1), however, when I submit it, it return "compile error"

I'm not sure what I'm doing wrong below so that the "judge" doesn't like me =P

Code: Select all

#include <iostream>
using namespace std;

int main (int argc, char **argv){
    int maxcyclelength = 1;
    // Cycle through all values between argv[1] an argv[2]
    for(int min = atoi(argv[1]); min<atoi(argv[2]); min++){
            int currentlen = 1;
            int cnumber = min;
            // run the loop till the currentnumber is equal to 1
            while(cnumber != 1){
                                if( cnumber%2 != 0)
                                    cnumber = 3*cnumber+1;
                                    else
                                    cnumber = cnumber/2;
                                currentlen++;
            }
            if (currentlen > maxcyclelength)
                           maxcyclelength = currentlen;
            currentlen = 0;
    }
    cout<<atoi(argv[1])<<" "<<atoi(argv[2])<<" "<<maxcyclelength;
    return 0;
}    

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Thu Feb 10, 2005 9:26 am

The judge's compiler does not support the function atoi()

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Thu Feb 10, 2005 6:14 pm

Add this line at the begining of your program: #include <cstdlib>

Post Reply

Return to “Volume 1 (100-199)”