Posted: Tue Jun 27, 2006 5:55 pm
Isn't that for the c++ language though? How could I do that in c?
Code: Select all
int main() {
int i,j;
while(cin>>i>>j) {
if (i<j)
cout<<i<<" "<<j<<" "<<max_cyclic(i,j)<<endl;
else
cout<<i<<" "<<j<<" "<<max_cyclic(j,i)<<endl;
}
return 0;
}
Code: Select all
while(!cin.eof()) {
cin>>i>>j;
if (i<j)
cout<<i<<" "<<j<<" "<<max_cyclic(i,j)<<endl;
else
cout<<i<<" "<<j<<" "<<max_cyclic(j,i)<<endl;
}
return 0;
}
Code: Select all
#include <stdio.h>
int tabla[60000];
int cycle(unsigned long n)
{
int c;
if (n==1)
c=1;
else
if (n<60000)
if (tabla[n]!=-1)
c=tabla[n];
else
{
if (n%2==0)
c=cycle(n/2)+1;
else
c=cycle(3*n+1)+1;
tabla[n]=c;
}
else
if (n%2==0)
c=cycle(n/2)+1;
else
c=cycle(3*n+1)+1;
return c;
}
int cmax(unsigned long i, unsigned long j)
{
int maxim=cycle(i);
int aux;
unsigned long k;
if (i>j)
{
aux=j;
j=i;
i=aux;
}
for (k=i+1;k<=j;k++)
{
aux=cycle(k);
if (aux>maxim)
maxim=aux;
}
return maxim;
}
int main()
{
register int l;
unsigned long i,j;
for (l=2;l<60000;l++)
tabla[l]=-1;
tabla[1]=1;
while (2==scanf("%u %u",&i,&j))
{
printf("%u %u %d\n",i,j,cmax(i,j));
}
return 0;
}
Code: Select all
4 3
3 4
Code: Select all
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a,b;
while (cin>>a>>b)
{
int e,f;
int c=0;
if(b>a)
{ e=b;
f=a;
}
else
{ e=a;
f=b;
}
for (int n=f;n<=e;n++)
{
int s=0;
int i;
i=n;
while (i!=1)
{
if (i%2==1)
{i=i+i+i+1;
s++;}
else
{ i=i/2;
s++;
}
}
if (s>c)
c=s;
s=0;
}
c+=1;
cout<<a<<" "<<b<<" "<<c<<endl;
}
return 0;
}
Code: Select all
20 20
9999 9999
1 9999
340 3000
3000 340
500 101
1 1
9999 9998
Code: Select all
20 20 8
9999 9999 92
1 9999 262
340 3000 217
3000 340 217
500 101 144
1 1 1
9999 9998 92
Code: Select all
#include <stdio.h>
int cycle (int n) {
int i=0;
while (n>0) {
i++;
if (n==1) break;
if (n%2 == 1) n=3*n +1;
else n=n/2;
}
return i;
}
int main () {
int n1,n2,i,max,l=0,a[10000],f,g,p;
while (scanf ("%d %d\n", &n1, &n2)!=EOF) {
if (n2<n1) {
f=n1;
g=n2;
}
else {
f=n2;
g=n1;
}
max = cycle(i);
for (i=g;i<=f;i++) {
l=cycle(i);
if (l>max) max=l;
}
printf ("%d %d %d",n1,n2,max);
}
return 0;
}
your program gets tle,diego andres de barros wrote:my problems is wa.....
i believe the input is wrong.........
see ,please !!!!!!!!!
Code: Select all
while( scanf("%lld %lld",&i,&j) ){
Code: Select all
while( scanf("%lld %lld",&i,&j)==2 ){
Code: Select all
max = cycle(0); //always took the length of 0 as minimum
for (i=g;i<=f;i++) {
l=cycle(i);
if (l>max) max=l;
}
printf ("%d %d %d\n",n1,n2,max);