Code: Select all
"Case %d: A = %d, limit = %d, number of terms = %d\n"
Moderator: Board moderators
Code: Select all
"Case %d: A = %d, limit = %d, number of terms = %d\n"
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;
}
}
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;
}
}
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.Tanzim-Saqib wrote:Code: Select all
1 1 2147483647 1 2147483647 2 2147483647 214748364 1 0
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;
}
ur answer for tc 1 is ==> Case 1: A = 1, limit = 100, number of terms = 8NIPU 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++;
}
}
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;
}
}