It is a simple problem.
But the test data may be reach the limit.
so you'd better use the long long int not the int
Moderator: Board moderators
3 100
34 100
75 250
27 2147483647
101 304
101 303
715827883 2147483647
715827882 2147483647
715827881 2147483647
2000 100000001
31003 100000001
99999 500000001
99999 1901000001
99999 2141000001
99997 2141000001
7771 2141000001
7773 2141000003
77799 2141900
-2 -4Case 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
Case 7: A = 715827883, limit = 2147483647, number of terms = 1
Case 8: A = 715827882, limit = 2147483647, number of terms = 33
Case 9: A = 715827881, limit = 2147483647, number of terms = 6
Case 10: A = 2000, limit = 100000001, number of terms = 113
Case 11: A = 31003, limit = 100000001, number of terms = 161
Case 12: A = 99999, limit = 500000001, number of terms = 227
Case 13: A = 99999, limit = 1901000001, number of terms = 227
Case 14: A = 99999, limit = 2141000001, number of terms = 227
Case 15: A = 99997, limit = 2141000001, number of terms = 90
Case 16: A = 7771, limit = 2141000001, number of terms = 115
Case 17: A = 7773, limit = 2141000003, number of terms = 40
Case 18: A = 77799, limit = 2141900, number of terms = 39#include<iostream.h>
//#include<fstream.h>
void main(){
//ifstream cin("694.in");
long long A,L,terms,Case,mainA;
cin>>A>>L;
Case=0;
mainA=A;
while(A>0){
Case++;
terms=1;
while(true){
if(A==1)break;
if(A%2==0){A/=2;terms++;}
else{
if(A>((L-1)/3))break;
else {A=A*3+1;terms++;}
}
}
cout<<"Case "<<Case<<": A = "<<mainA<<", limit = "<<L<<", number of terms = "<<terms<<endl;
cin>>A>>L;
}
}
...
while(A>0){
mainA=A;
...
}"Case %d: A = %d, limit = %d, number of terms = %d\n"#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;
}
}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;
}
}
Users browsing this forum: No registered users and 1 guest