Page 37 of 93
Restricted Function error #100 3n+1
Posted: Sun Jan 30, 2005 10:24 pm
by jaredjohnson
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;
}
}
Posted: Mon Jan 31, 2005 2:31 am
by eleusive
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.
thanks
Posted: Wed Feb 02, 2005 1:47 am
by jaredjohnson
I fixed it.
Posted: Wed Feb 02, 2005 3:21 pm
by mohiul alam prince
Hi
why are u not solving this problem by using c/c++
this problem is easy for c/c++
MAP
Why this Compile Error
Posted: Thu Feb 03, 2005 5:47 am
by ImLazy
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???
Posted: Thu Feb 03, 2005 5:56 am
by UFP2161
alloc.h is not part of the standard libraries.
Posted: Thu Feb 03, 2005 10:19 am
by ImLazy
So do you mean I can't use alloc.h for any problem? OK,I see.
Posted: Thu Feb 03, 2005 1:46 pm
by sumankar
If I am not mistaken malloc() and the other similar fns are defined in stdlib.h.
Use it.
Regards,
Suman.
100 please
Posted: Sat Feb 05, 2005 2:41 pm
by carbing
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.
Posted: Sat Feb 05, 2005 3:51 pm
by Eduard
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
Posted: Sat Feb 05, 2005 4:00 pm
by carbing
Thank you for your useful help.I think i have to learn more about the C++ language.Thanks again.
time out.
Posted: Sun Feb 06, 2005 4:14 am
by jonaskoelker
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.
Problem: 100 compile error O.o;
Posted: Wed Feb 09, 2005 5:25 pm
by Insidetheasylum
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;
}
Posted: Thu Feb 10, 2005 9:26 am
by shamim
The judge's compiler does not support the function atoi()
Posted: Thu Feb 10, 2005 6:14 pm
by chunyi81
Add this line at the begining of your program: #include <cstdlib>