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

zobayer
Experienced poster
Posts: 110
Joined: Tue May 06, 2008 2:18 pm
Location: CSE-DU, Bangladesh
Contact:

Re: 100 The 3n+1 Problem - WA

Post by zobayer »

what is your output for

Code: Select all

1 1
It should be

Code: Select all

1 1 1
And to check EOF, this is better, because, sometimes, file may contain trailing garbage

Code: Select all

while(cin >> x >> y) {
}
or
while(scanf("%d %d", &x, &y)==2) {
}
Output format is ok I think.

AND BEFORE OPENING NEW THREADS, SEARCH THE FORUM FIRST FOR EXISTING THREADS, AND IF POSSIBLE, USE THOSE.
You should not always say what you know, but you should always know what you say.
thomgil
New poster
Posts: 2
Joined: Mon Jun 20, 2011 8:25 pm

Re: 100 Time Limit Exceeded

Post by thomgil »

Java is slower than C++
in most cases that doesn't matter
An Overview of Mobile Phone Signal Booster
Mobile Phone Signal Booster[/url] a Solution to Poor Signal? http://gizmolord.com/mobile-phone-signa ... or-signal/
thelite
New poster
Posts: 1
Joined: Sat Jun 25, 2011 12:10 am
Location: United States
Contact:

lite

Post by thelite »

I am not sure coz i am new in this forum as you may notice :)
nt90200
New poster
Posts: 2
Joined: Thu Jun 30, 2011 6:53 pm

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

Post by nt90200 »

I write it in C.
Can anyone help me with this?
WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int howlong(int q)
{    int a;
    a=0;  
    qwe:
    if(q==1)
    {goto asd;
            }
    else if(q%2==1)
    {q=3*q+1;
         }
    else
    {q=q/2;
        }
    a++;
    
    goto qwe;
    asd:
    a++;

    return a; 
     
}
main()
{
    int a,b,c,i,j;
    
    
    scanf("%d %d",&i,&j);
      if(i>j)
     {c=i;
      i=j;
      j=c;      
               
    
    
    
    a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      c=i;
      i=j;
      j=c;
    printf("%d %d %d",i,j,a);
    }

  else{
  a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      printf("%d %d %d",i,j,a); 
       }  
    return 0;
}
thefourtheye
New poster
Posts: 5
Joined: Sat Jun 04, 2011 9:36 am

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

Post by thefourtheye »

nt90200 wrote:I write it in C.
Can anyone help me with this?
WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int howlong(int q)
{    int a;
    a=0;  
    qwe:
    if(q==1)
    {goto asd;
            }
    else if(q%2==1)
    {q=3*q+1;
         }
    else
    {q=q/2;
        }
    a++;
    
    goto qwe;
    asd:
    a++;

    return a; 
     
}
main()
{
    int a,b,c,i,j;
    
    
    scanf("%d %d",&i,&j);
      if(i>j)
     {c=i;
      i=j;
      j=c;      
               
    
    
    
    a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      c=i;
      i=j;
      j=c;
    printf("%d %d %d",i,j,a);
    }

  else{
  a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      printf("%d %d %d",i,j,a); 
       }  
    return 0;
}
Try printing like this

printf("%d %d %d\n",i,j,a);
nt90200
New poster
Posts: 2
Joined: Thu Jun 30, 2011 6:53 pm

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

Post by nt90200 »

thefourtheye wrote:
nt90200 wrote:I write it in C.
Can anyone help me with this?
WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int howlong(int q)
{    int a;
    a=0;  
    qwe:
    if(q==1)
    {goto asd;
            }
    else if(q%2==1)
    {q=3*q+1;
         }
    else
    {q=q/2;
        }
    a++;
    
    goto qwe;
    asd:
    a++;

    return a; 
     
}
main()
{
    int a,b,c,i,j;
    
    
    scanf("%d %d",&i,&j);
      if(i>j)
     {c=i;
      i=j;
      j=c;      
               
    
    
    
    a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      c=i;
      i=j;
      j=c;
    printf("%d %d %d",i,j,a);
    }

  else{
  a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      printf("%d %d %d",i,j,a); 
       }  
    return 0;
}
Try printing like this

printf("%d %d %d\n",i,j,a);

It still WA
GOLDENMASTER
New poster
Posts: 1
Joined: Wed Sep 21, 2011 4:41 pm

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

Post by GOLDENMASTER »

i got WA can anyone help me out
[//#include<conio.h>
#include<iostream>
int table[10000000]={0};
int count(unsigned int);
main()
{
int s[1000],e[1000],max[1000]={1},j;
unsigned int i=0;
while(!std::cin.eof())
{
//int i=0;
std::cin>>s>>e;
j=i;
i++;}
for(int k=0;k<j;k++)
{
for(int i=s[k];i<=e[k];i++)
{
count(i);
}
for(int i=s[k];i<=e[k];i++)
{
if(max[k]<table)
{
max[k]=table;
}
}
//string ss=(string)s;
std::cout<<s[k]<<" "<<e[k]<<" "<<max[k]<<"\n";
}
//getch();
}
int count(unsigned int i)
{if(i<=10000000)
{
if(table==0)
{
if(i%2==0)
{
table=count(i/2)+1;
}
else if(i==1)
{
return 1;
}
else
{
table=count((3*i)+1)+1;
}
}
return table;
}else
{
if (i%2==0)
return(count(i/2)+1);
else
return(count((3*i)+1)+1);}
}
]
tuxskywalker
New poster
Posts: 1
Joined: Thu Oct 13, 2011 12:50 am

3n + 1

Post by tuxskywalker »

Rather a newbie at ANSI C. :oops:

I submitted this code for 3n+1 problem and get WA. :cry: Any suggestions?

#include <stdio.h>

int cycle(int n)
{
int count = 1;
while(n != 1)
{
if(n%2==0){ n /= 2; }
else{ n = (3*n)+1; }
++count;
}
return count;
}


int main()
{

int i,a,b;
scanf("%d %d", &a, &b);
while(!feof(stdin))
{
int test, result = 0;
for(i=a; i<=b; ++i)
{
test = cycle(i);
if( test > result){ result = test; }
}
printf("%d %d %d\n", a, b, result);
scanf("%d %d", &a, &b);
}
return 0;

}
zachaakelly
New poster
Posts: 1
Joined: Sat Oct 15, 2011 7:49 am

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

Post by zachaakelly »

I've been tinkering with this for a while, and I keep getting WA with almost no idea why

Code: Select all

[#include <stdio.h>

unsigned int i, j, num1, count, count1;
unsigned int backw = 0;
unsigned long num2;

unsigned long nextnum(int n){
    if(n%2==0){
        return n/2;
    } else{
        return 3*n+1;
    }
}

int main(void) {
    while(scanf("%d %d", &i, &j)==2){;
    if(i>j){
        num1 = i;
        i = j;
        j = num1;
        backw = 1;
    }
    for(num1=i;num1<=j;num1++){
        num2 = (long)num1;
        count = 1;
        while(num2!=1){
            num2=nextnum(num2);
            count++;
        }
        if (count>count1){
            count1 = count;
        }
    }
    if(!backw){
        printf("%d %d %d\n",i,j,count1);
    } else {
        printf("%d %d %d\n",j,i,count1);
    }
    count1 = 0;
    }
    return 0;
}
/code]
qiusha
New poster
Posts: 1
Joined: Fri Dec 16, 2011 5:28 am

100 java runtime error

Post by qiusha »

First time using Uva, got serious run time error problem, can anyone help, thanks.

import java.io.*;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
class Main{
static long max =0;
public static void three(long x, long y){
long i = 0;
long k =0;
for(long j=y;j<=x;j++){
k=j;
i++;
while(k!=1){
if(k%2==0){
k=k/2;
i++;
}
else{
k=3*k+1;
i++;
}
}
if (i>max){
max=i;
}
i=0;
}
}
public static void main(String args[]){
String thisLine;
ArrayList<String> al = new ArrayList<String>();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((thisLine = br.readLine()).length() != 0){
StringTokenizer st = new StringTokenizer(thisLine);
if(st.hasMoreTokens()){
String a = st.nextToken();
String b =st.nextToken();
al.add(a);
al.add(b);
}
}
for(int i=0; i<al.size()-1; i=i+2){
long y = Long.parseLong(al.get(i));
long x = Long.parseLong(al.get(i+1));
if(x>=y){
three(x,y);
if(i==al.size()-2){
System.out.print(y+" "+x+" "+max);
}
else{
System.out.println(y+" "+x+" "+max);
}
max=0;
}
else{
three(y,x);
if(i==al.size()-2){
System.out.print(y+" "+x+" "+max);
}
else{
System.out.println(y+" "+x+" "+max);
}
max=0;
}
}
System.exit(0);
}catch(IOException e){
System.exit(1);
}
}
}
javacom
New poster
Posts: 3
Joined: Sun Dec 25, 2011 6:05 pm

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

Post by javacom »

Could you please tell me why I get WY with this code ? :(

Code: Select all

#include <iostream>
#include <algorithm>


using namespace std;
unsigned long int calc(unsigned long int val){
	int counter=1;
	while(val!=1){
		if(val%2==0){
			val=val/2;
			counter++;
		}
		else{
			val=val * 3 +1;
			counter++;
		}
	}
	return counter;
}
int main() {
	unsigned long int start, startCount, end, endCount, maxCount;

	while(cin>> start >> end){
	startCount=start;
	endCount = end;
	unsigned long int maxNum=max(start, end);
	unsigned long int minNum=min(start, end);
	maxCount = calc(minNum);
	while(minNum != maxNum){
		maxCount = max(maxCount, calc(minNum));
		++minNum;
	}
	cout << startCount << " " << endCount << " " << maxCount << endl;
	}
	return 0;
}
javacom
New poster
Posts: 3
Joined: Sun Dec 25, 2011 6:05 pm

Please Help, why I am getting WA (100) c++ ?

Post by javacom »

could you please tell me why I'm getting WY (100 3n+1 problem) with this code ?

Code: Select all

#include <iostream>
#include <algorithm>


using namespace std;
unsigned long int calc(unsigned long int val){
   int counter=1;
   while(val!=1){
      if(val%2==0){
         val=val/2;
         counter++;
      }
      else{
         val=val * 3 +1;
         counter++;
      }
   }
   return counter;
}
int main() {
   unsigned long int start, startCount, end, endCount, maxCount;

   while(cin>> start >> end){
   startCount=start;
   endCount = end;
   unsigned long int maxNum=max(start, end);
   unsigned long int minNum=min(start, end);
   maxCount = calc(minNum);
   while(minNum != maxNum){
      maxCount = max(maxCount, calc(minNum));
      ++minNum;
   }
   cout << startCount << " " << endCount << " " << maxCount << endl;
   }
   return 0;
}
helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

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

Post by helloneo »

minNum != maxNum

should be

minNum <= maxNum
javacom
New poster
Posts: 3
Joined: Sun Dec 25, 2011 6:05 pm

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

Post by javacom »

helloneo wrote:minNum != maxNum

should be

minNum <= maxNum
Thank you, it works. AC finally :)
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 100 java runtime error

Post by brianfry713 »

change this line from:
while((thisLine = br.readLine()).length() != 0){
to this:
while((thisLine = br.readLine()) != null){

Trying to take the length of a null String throws a NullPointerException.

Also change to a println every time and you'll get AC.

Paste your code inside code blocks and use the existing thread for a problem.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 1 (100-199)”