100 - The 3n + 1 problem

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

gapsonhk
New poster
Posts: 5
Joined: Wed Mar 22, 2006 7:08 am

Post by gapsonhk »

fpavetic wrote:check this
1 1

output should be 1, but your program outputs 2
passed
enib
New poster
Posts: 1
Joined: Tue Apr 04, 2006 6:02 pm

Post by enib »

mine works.. dunno why I GET WRONG ANSWER..

eni@madgeek:~/Documents/projects/programim/C/problems$ cat 100.c
/*****************
* problem no. 100
* Programmed by:
* Eni Bundo
* ****************/

#include <stdio.h>
#define small(x, y) ((x<y) ? x : y)
#define max(x, y) ((x>y) ? x : y)
int main() {
int bigN, smallN, k;
int j, i, counter=0, c;
int maxcyclelength=0;
scanf ("%d %d", &i, &j);
smallN=small(i, j);
bigN=max(i, j);

for (k=smallN; k<=bigN; k++) {
c=k;
counter=0;
while (c != 1) {
if (c % 2)
c = 3*c + 1;
else
c = c / 2;
counter++;
}
counter++;
if(counter>maxcyclelength)
maxcyclelength=counter;
}
printf ("%d %d %d",i, j, maxcyclelength);
return 0;
}
eni@madgeek:~/Documents/projects/programim/C/problems$ gcc 100.c
eni@madgeek:~/Documents/projects/programim/C/problems$ ./a.out
1 10
1 10 20eni@madgeek:~/Documents/projects/programim/C/problems$ ./a.out
100 200
100 200 125eni@madgeek:~/Documents/projects/programim/C/problems$
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

There can be more than one test case enib.

What does your code output for:

Code: Select all

1 10
100 200
Enter the above two cases together, not separately. Your code will give all output in one line. You need to print a newline for each line of output.
pongdanai
New poster
Posts: 2
Joined: Sat Apr 08, 2006 3:28 pm

help WA! 100

Post by pongdanai »

I submit my code and .... Wrong Answer here is my code

#include<stdio.h>
#define MAX(a,b) a<b?b:a
int fnum[10],lnum[10],res[10],count=0;
int chk(long num)
{int count2=1;
while(num!=1)
{ if(num%2!=0)num=(num*3)+1 ;
else num=num/2;
count2++;
}
return count2;
}

int main()
{int i;
int ans=0,t;
while(scanf("%ld %ld",&fnum[count],&lnum[count])!=EOF)
{if(fnum[count]>lnum[count])
{t=fnum[count];
fnum[count]=lnum[count];
lnum[count]=t;
}
for(i=fnum[count];i<lnum[count];i++)
{ans=MAX(ans,chk(i));
}
res[count]=ans;
ans=0;
count++;
}
for(i=0;i<count;i++)printf("%ld %ld %d\n",fnum,lnum,res);
return 1;
}
Artikali
Learning poster
Posts: 68
Joined: Wed Sep 21, 2005 5:27 pm

Post by Artikali »

input
10 1
yours
1 10 20
correct one is
10 1 20
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Also what if there are more than 10 test cases in the input? Your code will give RTE. Try to read and process one input at a time.
pongdanai
New poster
Posts: 2
Joined: Sat Apr 08, 2006 3:28 pm

Helppp Why? WA

Post by pongdanai »

I don't know why it wrong here is my cod plzz help

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]
kodder
New poster
Posts: 6
Joined: Mon Apr 10, 2006 6:19 am
Location: china
Contact:

100 RE help me???

Post by kodder »

why my code get runtime error, it works well in my computer
can anyone help me???

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;
}

kodder
New poster
Posts: 6
Joined: Mon Apr 10, 2006 6:19 am
Location: china
Contact:

this is 101 problems!!

Post by kodder »

:lol: :lol: :lol:
salahuddin66
New poster
Posts: 1
Joined: Sat Apr 29, 2006 9:32 am

100 run time err

Post by salahuddin66 »

hello,

why i am getting run time err :(
any hint

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;

}

-salahuddin66
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

it is forbidden to make a new thread when there is already a thread about the topic. .. at least at the case of problem number 100

your array size is too big.. thats why u gets run time error
prodigyt
New poster
Posts: 3
Joined: Sun Apr 30, 2006 6:35 pm

Post by prodigyt »

so you'd better edit your first post and change its title ...
felicitas
New poster
Posts: 1
Joined: Wed May 03, 2006 10:52 am
Contact:

100 - someone plz reply :(

Post by felicitas »

I tried the 3n+1 problem with .. c and it is giving variables not declared error... I followed this format

int main()
{
//code
return 0;
}

.. then fed up .. I tried it in java.. it is giving restricted function access(system) .. error.

In java everybody needs System.out.println(); to print the output.. incase you need more clarity regarding my problem.. let me know.. then I'll post the code.. that I submitted(if it is not against the rules)...

Felicitas
SiLwS
New poster
Posts: 6
Joined: Wed May 03, 2006 8:12 pm

100 Runtime error

Post by SiLwS »

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;
}
This was the first thread about 100 ...
Why the runtime error ? I mean I tested for 1000000 and it was quite fast... =/
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

SiLwS,
Do you think,
there have not more than 100 pair of input?
If there are more than 100 then how does your array take those input

so for input use only two variable, rather than array.
Post Reply

Return to “Volume 1 (100-199)”