100 - The 3n + 1 problem
Moderator: Board moderators
-
- New poster
- Posts: 11
- Joined: Sun Jan 30, 2005 10:21 pm
Restricted Function error #100 3n+1
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;
}
}
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;
}
}
-
- Experienced poster
- Posts: 120
- Joined: Sat Nov 01, 2003 6:16 am
- Location: Dhaka (EWU)
Why this Compile Error
I submit this code for problem #100. Not to solve the problem, but just a test.
But get Compile Error. Why???
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;
}
I stay home. Don't call me out.
100 please
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.
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.
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.
or
Eduard
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 //
}
Code: Select all
While (true) {
cin >> a >> b;
// Your solution //
}
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
-
- New poster
- Posts: 7
- Joined: Fri Jan 28, 2005 10:01 pm
- Location: Denmark
- Contact:
time out.
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.
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.
-
- New poster
- Posts: 2
- Joined: Wed Feb 09, 2005 5:24 pm
Problem: 100 compile error O.o;
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
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;
}