Posted: Sun Jun 03, 2007 5:47 pm
Code: Select all
if (!cin) break;
Code: Select all
if (!cin) break;
Code: Select all
while (cin >> a >> b)
{
// process input
}
Code: Select all
#include <stdio.h>
#include <math.h>
float max_ciclos(float);
int main(int argc,char **argv){
unsigned long int i,j,k,a,b;
float c,aux;
float vetor[1000000];
for (k=1;k<1000000;k++)
vetor[k]=max_ciclos((float)k);
while((scanf("%lu %lu",&i,&j))!=EOF){
c=0.0;
if (i>j){
a=j;
b=i;
} else {
a=i;
b=j;
}
for (k=a;k<=b;k++){
aux=vetor[k];
if (aux>c)
c=aux;
}
printf("%lu %lu %0.0f\n",i,j,c);
}
return 0;
}
float max_ciclos(float n){
float ciclos,t;
ciclos=0.0;
t=0.0;
while (n>1.0){
ciclos=ciclos+1.0;
if (n==1.0) break;
else{
t=(n/2.0)-floor(n/2.0);
if (t>0.0)
n=(3.0*n)+1.0;
else
n=n/2.0;
}
}
ciclos=ciclos+1.0;
return ciclos;
}
Code: Select all
#include <stdio.h>
#include <math.h>
float vetor[1000000];
float max_ciclos(float);
int main(int argc,char **argv){
unsigned long int i,j,k,a,b;
float c,aux;
for (k=0;k<1000000;k++)
vetor[k]=0.0;
while((scanf("%lu %lu",&i,&j))!=EOF){
c=0.0;
if (i>j){
a=j;
b=i;
} else {
a=i;
b=j;
}
if ((a>0) && (a<1000000))
if ((b>0) && (b<1000000))
for (k=a;k<=b;k++){
aux=max_ciclos((float)k);
if (aux>c)
c=aux;
}
printf("%lu %lu %0.0f\n",i,j,c);
}
return 0;
}
float max_ciclos(float n){
float ciclos,t;
long int v;
ciclos=0.0;
t=0.0;
v = (long int) n;
if (vetor[v]!=0.0)
return vetor[v];
while (n>1.0){
ciclos=ciclos+1.0;
t=(n/2.0)-floor(n/2.0);
if (t>0.0)
n=(3.0*n)+1.0;
else
n=n/2.0;
}
ciclos=ciclos+1.0;
vetor[v]=ciclos;
return ciclos;
}
Code: Select all
const maxn=1000000;
var
i,a,b,max:longint;
l:array[0..maxn] of longint;
function tryl(n:int64;t:longword):longint;
var
nextn:int64;len:longint;tmp:extended;
begin
if n<=maxn then begin
if l[n]>0 then tryl:=l[n]+t else begin
if n mod 2=0 then tmp:=n div 2 else tmp:=n*3+1;
nextn:=round(tmp);
len:=tryl(nextn,t+1);
tryl:=len;
if n<=maxn then l[n]:=len-t;
end;
end else begin
if n mod 2=0 then tmp:=n div 2 else tmp:=n*3+1;
nextn:=round(tmp);
len:=tryl(nextn,t+1);
tryl:=len;
if n<=maxn then l[n]:=len-t;
end;
end;
begin
l[1]:=1;
for i:=2 to maxn do begin
l[i]:=tryl(i,0);
end;
while true do begin
if eof() then break;
readln(a,b);
max:=0;
if a<=b then begin
for i:=a to b do begin
if l[i]>max then max:=l[i];
end;
end else begin
for i:=b to a do begin
if l[i]>max then max:=l[i];
end;
end;
writeln(a,' ',b,' ',max);
end;
end.
Code: Select all
#include <stdio.h>
main()
{
int i , j , num , temp , iOriginal , jOriginal , max_length = 0 ;
while(scanf("%d %d",&i,&j) == 2)
{
iOriginal = i ;
jOriginal = j ;
//Swap i and j if out of order
if(i > j)
{
temp = i ;
i = j ;
j = temp ;
}
for(num = i ; num <= j ; num++)
{
temp = calculate_cycle_length(num);
if(temp > max_length)
max_length = temp ;
}
printf("%d %d %d\n",iOriginal,jOriginal,max_length);
max_length = 0 ;
}
}
int calculate_cycle_length(int num)
{
int cycle_length = 0 ;
do
{
if(num%2 == 0)
num/=2 ;
else
num = (num*3)+1 ;
cycle_length++ ;
}while(num != 1);
cycle_length++ ; //Count the 1 at the end too
return cycle_length ;
}
It can represent exactly only about 5 most-significant digits. The rest are lost in rounding.So float is able to carry much more...
There's no standard data type to hold it, that doesn't round most of these digits right away.how can I store a number like 11111111111111111111111111111111111111111111111111111111111.0 on a long double for instance?any tips?
Code: Select all
#include <stdio.h>
main()
{
long int arr[1000][2] , num1 , num2 ;
long int i , j , cycle_length , max_cycle_length =0 ;
int ctr = 0 ;
long int calculate_cycle_length(long int);
while(scanf("%ld %ld",&i,&j) != EOF)
{
arr[ctr][0] = i ;
arr[ctr][1] = j ;
ctr++ ;
}
for(i = 0 ; i < ctr ; i++)
{
if(arr[i][0] > arr[i][1])
{
num1 = arr[i][1] ;
num2 = arr[i][0] ;
}
else
{
num1 = arr[i][0] ;
num2 = arr[i][1] ;
}
for(j = num1 ; j <= num2 ; j++)
{
cycle_length = calculate_cycle_length(j);
if(cycle_length > max_cycle_length)
max_cycle_length = cycle_length ;
}
printf("\n%ld %ld %ld",arr[i][0],arr[i][1],max_cycle_length);
max_cycle_length = 0 ;
}
}
long int calculate_cycle_length(long int num)
{
long int cycle_length = 0 ;
do
{
if(num%2 == 0)
num/=2 ;
else
num = (num*3)+1 ;
cycle_length++ ;
}while(num != 1);
cycle_length++ ;
return cycle_length ;
}