694 - The Collatz Sequence

Moderator: Board moderators

Re:

Tanzim-Saqib wrote:
Code: Select all
`1 12147483647 12147483647 22147483647 2147483641 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.
Think FREE, Think OPEN SOURCE
devmehedee
New poster

Posts: 2
Joined: Sun Apr 17, 2011 11:52 am

Re: 694 - The Collatz Sequence

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;}`
You are the one, who can make a difference...
New poster

Posts: 4
Joined: Sat Apr 16, 2011 10:04 pm
Location: IIT, DU

Re: 694 - The Collatz Sequence

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++;
}
}
NIPU
New poster

Posts: 1
Joined: Thu Aug 11, 2011 11:01 pm

Re: 694 - The Collatz Sequence

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 ?
ignited
New poster

Posts: 1
Joined: Thu Sep 08, 2011 1:24 pm

Re: 694 - The Collatz Sequence

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
sumon sarker
New poster

Posts: 4
Joined: Tue Mar 20, 2012 7:58 am