694 - The Collatz Sequence

All about problems in Volume VI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re:

Postby devmehedee » Sun Apr 17, 2011 7:41 pm

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.
Think FREE, Think OPEN SOURCE
devmehedee
New poster
 
Posts: 2
Joined: Sun Apr 17, 2011 11:52 am
Location: CSE BUET, Bangladesh.

Re: 694 - The Collatz Sequence

Postby muctadir.dinar » Sun Jun 05, 2011 6:09 pm

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...
muctadir.dinar
New poster
 
Posts: 4
Joined: Sat Apr 16, 2011 10:04 pm
Location: IIT, DU

Re: 694 - The Collatz Sequence

Postby NIPU » Thu Aug 11, 2011 11:06 pm

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

Postby ignited » Thu Sep 08, 2011 1:30 pm

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

Postby sumon sarker » Mon Aug 27, 2012 4:46 am

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
Location: Dhaka,Bangladesh

Previous

Return to Volume VI

Who is online

Users browsing this forum: No registered users and 0 guests