Page 3 of 4

Posted: Wed Jan 10, 2007 10:37 pm
by Jan
Try my formatting...

Code: Select all

"Case %d: A = %d, limit = %d, number of terms = %d\n"
Hope it helps.

694 P.E.

Posted: Wed Jan 10, 2007 11:34 pm
by WR
Thanks a lot, Jan!

It DID help. And special thanks for the fast response!!!

Care to share your knowledge?

The format specifier for a LONG variable is %ld, isn't it?
Why does %d work instead?

Needless to say, my results were ok, otherwise the verdict would have been a WRONG ANSWER (:().

Posted: Wed Jan 10, 2007 11:39 pm
by Jan
You are welcome. I used int (not long) because in uva's compiler (and mine, too) int and long both require 32bit. May be that's why both identifier work for both (int & long).

Posted: Sat Mar 08, 2008 5:35 pm
by scott1991
I'm getting tle and have no idea on making it faster...can someone help please?

Code: Select all

#include<iostream>

int main()
{	
		unsigned int start, max, terms=1,test=1,A;
a:
	scanf("%u %u",&start,&max);
		if(start<0&&max<0)
		{return 0;}
		else
		{
			A=start;
		while(start<=max&&start!=1)
		{
			start = (start&1)==0?(start>>1):(start*3+1);
			terms++;
		}
		printf("Case %u: A = %u, limit = %u, number of terms = %u\n",test,A,max,terms);
		test++;terms=1;
		goto a;
		}
}

Posted: Sun Mar 09, 2008 12:32 am
by Jan
Both start and end are unsigned integers. So, you will not find -1 in the input. I mean if the input is -1 -1, then start and max both will be unsigned(-1) = 4294967295.

Re: 694 - The Collatz Sequence

Posted: Sun Nov 01, 2009 10:09 pm
by Kikuneechan
Heya. I'm doing this in Java. I've coded everything and everything prints out as the page says so, but I get TIme Limit Exceeded which is really aggravating.

Code: Select all

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner fred = new Scanner(System.in);
    int start;
    int limit;
    int terms;
    int a = 0;
    
    while (fred.hasNext()) {
      start = fred.nextInt();
      limit = fred.nextInt();
      if (start > 0) {
        terms = getLength(start, limit);
        a++;
      } else {
        break;
      }
      System.out.println("Case "+a+": A = "+start+", limit = "+limit+", number of terms = "+terms);
    }
  }
  
  public static int getLength(int x, int y) {
    int length = 1;
    while (x != 1) {
      if (x <= y) {
        if ( x % 2 == 0) {
          x = x / 2;
          length++;
        }else{
          x = x * 3 + 1;
          length++;
        }
      } else {
        length--;
        break;
      }
    }
    
    return length;
  }
}
Thankies~

Re:

Posted: Sun Apr 17, 2011 7:41 pm
by devmehedee
Tanzim-Saqib wrote:

Code: Select all

1 1
2147483647 1
2147483647 2
2147483647 214748364
1 0
In the problem, they said The initial value of A is always less than L. Then why are you giving these inputs? I got AC while my code gives different results for your inputs.

Re: 694 - The Collatz Sequence

Posted: Sun Jun 05, 2011 6:09 pm
by muctadir.dinar
Can someone tell where is problem in my code...???

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int main()
{
//    freopen("input.txt", "r", stdin);

    long long int a, limit;
    int caseCount = 0;
    while (scanf("%lld %lld", &a, &limit)!=EOF && a>-1 && limit>-1)
    {
        long long int aBack = a;

        caseCount++;

        int i = 0;
        for (; a!=1 && a<=limit;i++)
        {
            if (a%2 == 0)
            {
                a = a/2;
            }
            else if (a%2!=0)
            {
                a = (a*3)+1;
            }
        }


        printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n",caseCount, aBack, limit, i);
    }

    return 0;
}

Re: 694 - The Collatz Sequence

Posted: Thu Aug 11, 2011 11:06 pm
by NIPU
i've got wrong answer so many time that its starting to frustrate me. i cant really find my problem. can anyone tell me what i am doing wrong

#include<stdio.h>
void main()
{
long long int a,b,c,d;
d=1;
while(1)
{
scanf("%lld %lld",&a,&b);
if(a<0&&b<0)break;
c=1;
while(a>1)
{
if (a%2==0)a=a/2;
else a=3*a+1;
if(a>b)break;
c++;
}
printf("case %lld:",d);
printf(" A = %lld,",a);
printf(" limit = %lld,",b);
printf(" number of terms = %lld\n",c);
d++;
}
}

Re: 694 - The Collatz Sequence

Posted: Thu Sep 08, 2011 1:30 pm
by ignited
NIPU wrote:i've got wrong answer so many time that its starting to frustrate me. i cant really find my problem. can anyone tell me what i am doing wrong

#include<stdio.h>
void main()
{
long long int a,b,c,d;
d=1;
while(1)
{
scanf("%lld %lld",&a,&b);
if(a<0&&b<0)break;
c=1;
while(a>1)
{
if (a%2==0)a=a/2;
else a=3*a+1;
if(a>b)break;
c++;
}
printf("case %lld:",d);
printf(" A = %lld,",a);
printf(" limit = %lld,",b);
printf(" number of terms = %lld\n",c);
d++;
}
}
ur answer for tc 1 is ==> Case 1: A = 1, limit = 100, number of terms = 8
the right answer is Case 1: A = 3, limit = 100, number of terms = 8
see where's wrong there ? :)

Re: 694 - The Collatz Sequence

Posted: Mon Aug 27, 2012 4:46 am
by sumon sarker
Sample Input

3 100
34 100
75 250
27 2147483647
101 304
101 303
-1 -1

Sample Output

Case 1: A = 3, limit = 100, number of terms = 8
Case 2: A = 34, limit = 100, number of terms = 14
Case 3: A = 75, limit = 250, number of terms = 3
Case 4: A = 27, limit = 2147483647, number of terms = 112
Case 5: A = 101, limit = 304, number of terms = 26
Case 6: A = 101, limit = 303, number of terms = 1

I got AC with
Case 5: A = 101, limit = 304, number of terms = 2
For input with 101 304

694 - The Collatz Sequence - Time limit exceeded

Posted: Fri Nov 09, 2012 3:54 pm
by stadoblech

Code: Select all

import java.io.PrintWriter;
import java.util.Scanner;


public class Main { 
    
    public static void main(String[] args) throws Exception{ 
        
        Scanner scan = new Scanner(System.in);
        
        int A = 0;
        int limit = 0;
        
        while(scan.hasNextInt()) { 
            A = scan.nextInt();
            limit = scan.nextInt();
            if(A == -1 && limit == -1) { 
                return;
            }
            System.out.println("Case 1: A = "+A+", limit = "+limit+", number of terms = "+sequence(A, limit));
        }
    }
    
    private static int sequence(int A,int limit) { 
        int result = 0;

        
        while(A != 1){ 
            
            if(A > 2147483647 && limit > 2147483647) { 
                break;
            }
            
            
            if(A % 2 == 0) {                
                A = A/2;
                result++;
            } else { 
                A = (A * 3) + 1;
                result++;
            }
            
            if(A > limit) { 
                break;               
            }
            
            if(A == 1) { 
                result++;
            }

        }

        return result;

    }
}
anyone could help ?

Re: 694 - The Collatz Sequence - Time limit exceeded

Posted: Fri Nov 09, 2012 9:17 pm
by brianfry713
A line that contains two negative integers follows the last case. That doesn't mean they are -1.
Input 1 1, number of terms is 1.

uva 694

Posted: Sun Apr 28, 2013 12:42 am
by mahbubcseju
why my code is compile error
#include<stdio.h>
int main()
{
long long int a,l,m,k=0,A,n=0,temp;

while(scanf("%lld%lld",&A,&l)==2)
{
if(A<0&&l<0)
{
break;
}

if(A>l)
{
temp=A;
A=l;
l=temp;
}

k=0;
a=A;
while(a>1)
{
if(a%2==0)
{
a=a/2;
}
else
{
a=3*a+1;
}
k++;

if(a>l)
{
break;
}
}
if(a==1)
{
k=k+1;
}
n++;

printf("Case %lld:",n);

printf(" A = %lld,",A);
printf(" limit = %lld,",l);
printf(" number of terms = %lld\n",k);





}
return 0;
}

Re: uva 694

Posted: Fri May 17, 2013 12:43 am
by brianfry713
That is AC code. You can click "My Submissions" to see the reason for your Compile Error.