Posted: Sun Mar 26, 2006 12:06 am
passedfpavetic wrote:check this
1 1
output should be 1, but your program outputs 2
passedfpavetic wrote:check this
1 1
output should be 1, but your program outputs 2
Code: Select all
1 10
100 200
Code: Select all
Code: Select all
#include<stdio.h>
#define MAX(a,b) a>b?a:b
unsigned long num,num2;
unsigned long chk(long n)
{unsigned long count=1;
while(n!=1)
{ n=n%2!=0?3*n+1:n/2;
count++;
}
return count;
}
int main()
{unsigned long n,n2,i,j,ans=0,p;
while(scanf("%ld %ld",&num,&num2)!=EOF)
{ n=num;
n2=num2;
if(n>n2)
{p=n;
n=n2;
n2=p;
}
if(n2!=1)
{
for(i=n;i<=n2;i++)ans=MAX(ans,chk(i));
}
printf("%ld %ld %ld\n",num,num2,ans);
ans=0;
}
return 0;
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct block {
struct block *next;
int n;
} *B[MAX];
void output(int nblk)
{
struct block *t;
int i;
for (i = 0; i < nblk; i++) {
printf("%d:", i);
t = B[i];
while (t) {
printf(" %d", t->n);
t = t->next;
}
printf("\n");
}
}
int valid(int a, int b, int n, int *addra, int *addrb)
{
int i, af, bf;
struct block *t;
if (a == b) return 0;
for (i = 0; i < n; i++) {
af = bf = 0;
t = B[i];
while (t) {
if (t->n == a) {
af = 1;
*addra = i;
}
if (t->n == b) {
bf = 1;
*addrb = i;
}
t = t->next;
}
if (af && bf) return 0;
}
return 1;
}
void turnback(struct block *p)
{
struct block *t;
if (p == NULL) return;
t = p->next;
p->next = B[p->n];
B[p->n] = p;
turnback(t);
}
void moveonto(int a, int b, int n)
{
struct block *t, *p;
int da, db;
if (valid(a, b, n, &da, &db)) {
t = B[da];
p = NULL;
while (t->n != a) {
p = t;
t = t->next;
}
turnback(t->next);
if (p == NULL) B[da] = t->next = NULL;
else t->next = p->next = NULL;
p = B[db];
while (p->n != b) p = p->next;
turnback(p->next);
p->next = t;
}
}
void moveover(int a, int b, int n)
{
struct block *t, *p;
int da, db;
if (valid(a, b, n, &da, &db)) {
t = B[da];
p = NULL;
while (t->n != a) {
p = t;
t = t->next;
}
turnback(t->next);
if (p == NULL) B[da] = B[da]->next = NULL;
else t->next = p->next = NULL;
p = B[db];
while (p->next) p = p->next;
p->next = t;
}
}
void pileonto(int a, int b, int n)
{
struct block *t, *p, *q;
int da, db;
if (valid(a, b, n, &da, &db)) {
t = B[db];
p = NULL;
while (t->n != b) {
p = t;
t = t->next;
}
turnback(t->next);
p = B[da];
q = NULL;
while (p->n != a) {
q = p;
p = p->next;
}
if (q == NULL) {
t->next = B[da];
B[da] = NULL;
} else {
t->next = p;
p->next = NULL;
}
}
}
void pileover(int a, int b, int n)
{
struct block *t, *p;
int da, db;
if (valid(a, b, n, &da, &db)) {
t = B[da];
p = NULL;
while (t->n != a) {
p = t;
t = t->next;
}
if (p == NULL) B[da] = NULL;
else p->next = NULL;
p = B[db];
while (p->next) p = p->next;
p->next = t;
}
}
int main(void)
{
int i, a, b, nblks;
char c1[MAX], c2[MAX];
scanf("%d", &nblks);
for (i = 0; i < nblks; i++) {
B[i] = (struct block *)malloc(sizeof(struct block));
B[i]->n = i;
B[i]->next = NULL;
}
while (scanf("%s", c1) == 1 && strcmp(c1, "quit")) {
scanf("%d %s %d", &a, c2, &b);
if (a >= nblks || b >= nblks) continue;
if (!strcmp(c1, "move") && !strcmp(c2, "onto")) {
moveonto(a, b, nblks);
} else if (!strcmp(c1, "move") && !strcmp(c2, "over")) {
moveover(a, b, nblks);
} else if (!strcmp(c1, "pile") && !strcmp(c2, "onto")) {
pileonto(a, b, nblks);
} else if (!strcmp(c1, "pile") && !strcmp(c2, "over")) {
pileover(a, b, nblks);
}
}
output(nblks);
return 0;
}
Code: Select all
#include <stdio.h>
int main (void)
{
unsigned long int i, j, m, k = 1, n;
unsigned long int l[10000001];
unsigned long int p;
scanf("%lu", &i);
if(!(0<i && i<1000001))
{
scanf("%lu", &i);
}
scanf("%lu", &j);
if(!(0<j && j<1000001))
{
scanf("%lu", &j);
}
printf ("%lu\t %lu\t", i, j);
if (i > j)
{
p = i-j;
p = p + 1;
for (; j <= i; j++)
{
for (m = j; m > 1;)
{
if (m % 2 == 0)
m = m / 2;
else
m = 3 * m + 1;
k = k + 1;
}
l[n] = k;
n = n + 1;
k = 1;
}
}
else
{
p = j - i;
p = p + 1;
for (; i <= j; i++)
{
for (m = i; m > 1;)
{
if (m % 2 == 0)
m = m / 2;
else
m = 3 * m + 1;
k = k + 1;
}
l[n] = k;
n = n + 1;
k = 1;
}
}
for (n = 0; n < p; n++)
{
if (l[n] > l[n + 1])
{
l[n + 1] = l[n];
}
}
printf ("%lu\n", l[p]);
return 0;
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main()
{
long int a[100],b[100],x,i,j,k,c,d,n,max;
i=1;
while(scanf("%ld %ld",&a[i],&b[i])!=EOF)
{i++;}
i--;
for(j=1;j<=i;j++)
{
if(a[j]>b[j])
{ c=a[j];d=b[j];}
else
{ c=b[j];d=a[j];}
max=0;
for(k=d;k<=c;k++)
{
x=k;
n=1;
while(x!=1)
{ if(x%2==0)
{ x=x/2;n++;}
else
{ x=3*x+1;n++;}
}
if(n>max)
{max=n;}
}
printf("%ld %ld %ld\n",a[j],b[j],max);
}
system("Pause");
return 0;
}