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

pankaj trivedi
New poster
Posts: 11
Joined: Wed Jun 21, 2006 5:11 pm
Contact:

Post by pankaj trivedi »

Your program gives :


input:

Code: Select all

1 1 
1 2
1 3
1 4


output:

Code: Select all

1 1 3 
1 2 3
1 3 7
1 4 7
which is wrong

correct output for the above input should be:

Code: Select all

1 1 1
1 2 2
1 3 8
1 4 8
Anything other than Accepted is irritating,even Presentation Error

http://acm.uva.es/problemset/usersjudge.php?user=40301
DavidFromArmenia
New poster
Posts: 3
Joined: Tue Oct 03, 2006 1:41 pm

problem 100 3n + 1

Post by DavidFromArmenia »

I have a problem with wrong answer.
why?
#include <iostream>
using namespace std;

int main()
{
int i, j, k, count = 0, maxlength = 0;
while (cin >> i >> j){
if (i > j){
k = j;
j = i;
i = k;
}
if (i <= 0)
i = 1;
cout << i << ' ' << j << ' ';
////////////////////////////////////
while (i <= j ){
k = i;
count = 1;
while (k != 1){
if (k % 2 == 1)
k = k * 3 + 1;
else
k = k / 2;
count ++;
}
if (count > maxlength)
maxlength = count;
i++;
}
cout << maxlength << endl;
maxlength = 0;
count = 0;
///////////////////////////////////
}
return 0;
}
DavidFromArmenia
New poster
Posts: 3
Joined: Tue Oct 03, 2006 1:41 pm

problem 100 3n + 1

Post by DavidFromArmenia »

I have a problem with wrong answer.
why?
#include <iostream>
using namespace std;

int main()
{
int i, j, k, count = 0, maxlength = 0;
while (cin >> i >> j){
if (i > j){
k = j;
j = i;
i = k;
}
if (i <= 0)
i = 1;
cout << i << ' ' << j << ' ';
////////////////////////////////////
while (i <= j ){
k = i;
count = 1;
while (k != 1){
if (k % 2 == 1)
k = k * 3 + 1;
else
k = k / 2;
count ++;
}
if (count > maxlength)
maxlength = count;
i++;
}
cout << maxlength << endl;
maxlength = 0;
count = 0;
///////////////////////////////////
}
return 0;
}
pankaj trivedi
New poster
Posts: 11
Joined: Wed Jun 21, 2006 5:11 pm
Contact:

Post by pankaj trivedi »

input:

Code: Select all

1 2 
2 1
Your output:

Code: Select all

1 2 2 
1 2 2 
but output should be:

Code: Select all

1 2 2 
2 1 2 

so order of a and b should remain same
Anything other than Accepted is irritating,even Presentation Error

http://acm.uva.es/problemset/usersjudge.php?user=40301
DavidFromArmenia
New poster
Posts: 3
Joined: Tue Oct 03, 2006 1:41 pm

Re: If you get WA in problem 100, read me before post!

Post by DavidFromArmenia »

#include <iostream>
using namespace std;

int main()
{
int i, j, k, count = 0, maxlength = 0;
while (cin >> i >> j){
if (i > j){
k = j;
j = i;
i = k;
}
if (i <= 0)
i = 1;
cout << i << ' ' << j << ' ';
////////////////////////////////////
while (i <= j ){
k = i;
count = 1;
while (k != 1){
if (k % 2 == 1)
k = k * 3 + 1;
else
k = k / 2;
count ++;
}
if (count > maxlength)
maxlength = count;
i++;
}
cout << maxlength << endl;
maxlength = 0;
count = 0;
///////////////////////////////////
}
return 0;
}
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

input:

Code: Select all

20 10
output:

Code: Select all

20 10 21
pankaj trivedi
New poster
Posts: 11
Joined: Wed Jun 21, 2006 5:11 pm
Contact:

Post by pankaj trivedi »

a can be greater than b ,so consider that case also

input:

Code: Select all

20 10
your output:

Code: Select all

20 10 0
which is wrong.

btw, dont create new thread for existing one.
you can search before posting .
Anything other than Accepted is irritating,even Presentation Error

http://acm.uva.es/problemset/usersjudge.php?user=40301
Mr.V
New poster
Posts: 1
Joined: Wed Oct 04, 2006 9:29 pm

Yes..again 3n+1

Post by Mr.V »

Is there any errors in this code?? I don't think so, but it seems to be wrong...maybe someone could tell me what's the matter?

#include <iostream.h>


void main()
{


int n,k,i[100],j[100],t,mX[100],m=-1;



cout<<"Enter the numbers of period:\n";
while(true){
++m;
int ii,jj;
cin>>i[m]>>j[m];
if(i[m]>j[m]){
ii=j[m];
jj=i[m];
}
else {
ii=i[m];
jj=j[m];
}

if(ii>=1000000 || jj>=1000000 || ii<=0 || jj<=0)
break;

for(t=ii;t<=jj;t++){
for(n=t,k=1;n!=1;k++){
if(n%2!=0)
n=3*n+1;
else n=n/2;
}
if(k>mX[m])
mX[m]=k;
}
}

for(int y=0;y<m;y++)
cout<<i[y]<<" "<<j[y]<<" "<<mX[y]<<endl;

}
pankaj trivedi
New poster
Posts: 11
Joined: Wed Jun 21, 2006 5:11 pm
Contact:

Post by pankaj trivedi »

We already have threads for this problem , you will get your problem solved there ,please search before creating a new thread


BTW, Your program will not compile and there are errors in logic too . Read the problem statement carefully and use gnu/g++ compiler.
Anything other than Accepted is irritating,even Presentation Error

http://acm.uva.es/problemset/usersjudge.php?user=40301
DoDo@TW
New poster
Posts: 1
Joined: Sat Oct 07, 2006 12:10 pm

TE in 100

Post by DoDo@TW »

This is my code

Code: Select all

Cut after AC
But I got TE
Please help me
because I use #define???
Sayeef
New poster
Posts: 12
Joined: Sun Jun 18, 2006 3:06 am

3n+1 problem

Post by Sayeef »

Hi there.

Your code is ok but for this problem. When you swap i with j then if you print it comes in a reverse order(if i>j). Like
input:
1 2
2 1
output:
1 2 2
1 2 2
but second one should b 2 1 2. the exact order the input was given.

so you do this:

Code: Select all

int a,b;
a=i;b=j;
   
if (i > j){ 
k = j; 
j = i; 
i = k; 
} 
cout << a << ' ' << b << ' '; 
t_o_ohm
New poster
Posts: 2
Joined: Thu Oct 12, 2006 7:00 pm
Location: Debsirin School,Bangkok,Thailand

what's wrong on my program(Problem 100 3n+1)

Post by t_o_ohm »

Here is my C code
#include<stdio.h>
void main(){
long int m,n,i,j,k=1,max,l;
max=k;
scanf("%ld %ld",&i,&j);
if(i<j){
n=j;
m=i;
}
else if(i==j){
n=i;
m=j;
}
else{
n=i;
m=j;
}
l=n;
do{
n=l;
k=1;
p2:
if(k>=max){
max=k;
}
else{}
if(n==1){
}
else{
if(n%2==1){
n=(3*n)+1;
}
else{
n=n/2;
}
k++;
goto p2;
}
l--;
}while(l>=m);
printf("%ld %ld %ld \n",i,j,max);
}
I try sample input.it's work but why i got WA.thanks
helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

you're not supposed to make a thread about prob. 100

check this out..

http://online-judge.uva.es/board/viewtopic.php?t=3015
t_o_ohm
New poster
Posts: 2
Joined: Thu Oct 12, 2006 7:00 pm
Location: Debsirin School,Bangkok,Thailand

why i got WA

Post by t_o_ohm »

here is my source code

#include<stdio.h>
void main(){
unsigned long int m,n,i,j,k=1,max,l;
max=k;
scanf("%lu %lu",&i,&j);
if(i<j){
n=j;
m=i;
}
else if(i==j){
n=i;
m=j;
}
else{
n=i;
m=j;
}
l=n;
do{
n=l;
k=1;
p2:
if(k>=max){
max=k;
}
else{}
if(n==1){
}
else{
if(n%2==1){
n=(3*n)+1;
}
else{
n=n/2;
}
k++;
goto p2;
}
l--;
}while(l>=m);
printf("%lu %lu %lu \n",i,j,max);
}

i try all sample input it's work but i got WA. why?
richardxx
New poster
Posts: 3
Joined: Fri Oct 20, 2006 7:51 am

3n+1 again

Post by richardxx »

I don't know why, I test most cases yet..

#include <stdio.h>
#include <string.h>

int st[1000001];

int work(int org)
{
int cnt=0;
unsigned int start;

start=org;

while (start!=1) {
if (start<org && st[start]!=0) {
cnt+=st[start];
return st[org]=cnt;
}
if (start&0x1)
start=(start<<1)+start+1;
else
start>>=1;
++cnt;
}

return st[org]=cnt+1;
}

int main()
{
int m,n;
int i;
int max;
int temp;

memset(st,0,sizeof(st));
st[1]=1;
st[2]=2;

while (1) {
if (scanf("%d %d",&m,&n)==EOF)
break;
if (m>n) {
temp=m;
m=n;
n=temp;
}
max=0;
for (i=m;i<=n;++i) {
temp=work(i);
if (temp>max)
max=temp;
}
printf("%d %d %d\n",m,n,max);
}

return 0;
}
Post Reply

Return to “Volume 1 (100-199)”