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

amzad.ethan
New poster
Posts: 1
Joined: Thu Jan 30, 2014 6:22 pm
Location: Dhaka

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

Post by amzad.ethan » Thu Jan 30, 2014 6:37 pm

Here is my code. can anyone tell me why I have got WA???

Code: Select all

import java.util.Scanner;
class Main
{
  public static void main(String [] args)
  {
    Scanner e=new Scanner(System.in);
    while (e.hasNext())
    {
      int p=0;
      int r=0;
      int i= e.nextInt();
      int j = e.nextInt();
      if (j<i)
      {
        p=i;
        r=j;
        int temp=i;
        i=j;
        j=temp;
      }
      else 
      {
        p=i;
        r=j;
      }
      int [] a=new int [j+1];
      int m=i;
      while (m<=j)
      {
        int n=m;
        int c=1;
        while (n!=1)
        {
          if (n%2!=0)
          {
            n=3*n+1;
          }
          else 
          {
            n=n/2;
          }
          c++;
        }
        a[m]=c;
        m++;                  
      }
      int max=a[0];
      int c=1;
      while (c<j)
      {
        if (max<a[c])
        {
          max=a[c];
        }
        c++;
      }
      System.out.println(p+" "+r+" "+max);
    }
  }
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Post by brianfry713 » Thu Jan 30, 2014 10:32 pm

Input:

Code: Select all

1 1
1 2
2 2
Correct output:

Code: Select all

1 1 1
1 2 2
2 2 2
Check input and AC output for thousands of problems on uDebug!

gautamsingh
New poster
Posts: 5
Joined: Tue Apr 22, 2014 6:28 pm

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

Post by gautamsingh » Tue Apr 22, 2014 7:39 pm

I submitted this code about 20 minutes ago and still no verdict is being shown. Submission ID is 13533745 and same for 13533473 which was submitted about an hour ago. (this one's incorrect though)

Code: Select all

#include <algorithm>
#include <cstdio>
#define INF 9999999
using namespace std;
int a[1000050];
int coll(int p)
{
	int c = 1, temp = p;
	if (a[p] != 0)
		return a[p];
	else
	{
		while (temp != 1)
		{
			if (temp % 2 == 0)
				temp /= 2;
			else
				temp = 3*temp + 1;
			++c;	
		}
		a[p] = c;
	}
	return c;
}	
int main() 
{
	int i, j;
	while ((scanf ("%d%d", &i, &j)) != EOF)
	{
		int ans = -INF;
		for (int p = min(i, j); p <= max(i, j); ++p)
			ans = max(ans, coll(p));
		printf ("%d %d %d\n", i, j, ans);	
	}
	return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Post by brianfry713 » Tue Apr 22, 2014 8:27 pm

The judge is currently having issues, try again later.
Check input and AC output for thousands of problems on uDebug!

gautamsingh
New poster
Posts: 5
Joined: Tue Apr 22, 2014 6:28 pm

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

Post by gautamsingh » Wed Apr 23, 2014 6:06 am

brianfry713 wrote:The judge is currently having issues, try again later.
I got it accepted. The judge was too slow. Thanks for your help. Should I delete the code posted above?

samoel_stt
New poster
Posts: 8
Joined: Mon Jun 16, 2014 11:59 pm

Re: 100

Post by samoel_stt » Thu Jun 19, 2014 4:35 am

I keep getting wrong answer, what i am doing wrong?

Code: Select all

#include <stdio.h>
#include <stdlib.h>
int proceso(long num){
    int cont = 1;
    while( num != 1){
        if (num % 2 == 1) num = 3*num+1;
        else num = num/2;
        cont++;
    }   
    return cont;
}
void ciclo3n(int n1,int n2, int *max){
    int i;
    int maximo = proceso(n1);
    for(i=n1+1;i<=n2;i++){
        int cur = proceso(i);
        if(cur > maximo)
            maximo = cur;        
    }
    *max = maximo;
}
int main(int argc, char** argv) {
    long n1=0,n2=0;
    for(;;){
        if ( scanf("%d %d",&n1,&n2) == EOF) break;
        if (0<n1 && n1<1000000 && 0<n2 && n2<1000000){
            if (n1 > n2){
                int aux = n1;
                n1 = n2;
                n2 = aux;
            }
            int max=0;
            ciclo3n(n1,n2,&max);
            printf("%d %d %d\n",n1,n2,max);
        }        
    }
    return 0;
}

Shadow_Coder
New poster
Posts: 4
Joined: Mon Jun 16, 2014 2:19 pm

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

Post by Shadow_Coder » Thu Jun 19, 2014 10:45 am

Given below is my code. It works for all the test cases which I've tried, it swaps i and j, if i>j as well. Yet, it's still getting WA. Any suggestions?

Code: Select all

#include <iostream>
#include <algorithm>
using namespace std;

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

int main() {
    int i, j, I, J, Max;
    while(!cin.eof()){
    	cin >> i >> j;
    	cout << i << " " << j << " "; 
    	I = min(i, j); J = max(i, j);
    	Max = -1;
    	for(int k=I;k<=J;k++){
    		Max = max(Max, cycleLength(k)); 
    	}
    	cout << Max;
    	if(!cin.eof()) { cout << "\n"; }
    }
	return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 100

Post by brianfry713 » Thu Jun 19, 2014 10:20 pm

Input:

Code: Select all

9 1
AC output:

Code: Select all

9 1 20
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Post by brianfry713 » Thu Jun 19, 2014 10:21 pm

Change your loop to while(cin >> i >> j) {
Don't use cin.eof() while reading integers because the last line of the input will end with a newline char.
Also make sure you always print a newline char at the end of the last line.
Check input and AC output for thousands of problems on uDebug!

Shadow_Coder
New poster
Posts: 4
Joined: Mon Jun 16, 2014 2:19 pm

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

Post by Shadow_Coder » Fri Jun 20, 2014 3:51 pm

I got AC, but tell me, why do we need a newline character at the end of the last line, when it is not so in the sample output?

villainoftime
New poster
Posts: 1
Joined: Sat Jun 21, 2014 8:07 am

Re: 100

Post by villainoftime » Sat Jun 21, 2014 8:28 am

Why am I getting wrong answer, please??

#include<stdio.h>


int collatz(int n);
int cyclecounter(int n);

int main()
{
int i, j, cycle, MAX;
while(scanf("%d %d", &i, &j)==2)
{
MAX=0;
int n=i, p;
if(i>j)
p=-1;
else
p=1;
for(; n!=j; n+=p)
{
int cycle=cyclecounter(n);
if(MAX<cycle)
MAX=cycle;
}
printf("%d %d %d\n", i, j, MAX);
}
return 0;
}




int collatz(int n)
{
if(n%2==0)
return(n/2);
return(3*n+1);
}


int cyclecounter(int n)
{
int cycle=0;
n=collatz(n);
cycle++;
while(n!=1)
{
n=collatz(n);
cycle++;
}
return(cycle+1);
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Post by brianfry713 » Mon Jun 23, 2014 8:55 pm

If you want to get AC, always print a newline char at the end of the last line.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 100

Post by brianfry713 » Mon Jun 23, 2014 9:32 pm

Input 1 1, output should be 1 1 1
Check input and AC output for thousands of problems on uDebug!

sampad74
New poster
Posts: 29
Joined: Wed Jun 18, 2014 3:57 pm
Location: Bangladesh

UVa:100 why WA?

Post by sampad74 » Thu Jun 26, 2014 4:36 pm

Got AC
Last edited by sampad74 on Fri Jun 27, 2014 6:38 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Post by brianfry713 » Thu Jun 26, 2014 7:34 pm

i or j might be larger than 10000
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 1 (100-199)”