passedfpavetic wrote:check this
1 1
output should be 1, but your program outputs 2
100 - The 3n + 1 problem
Moderator: Board moderators
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$
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$
There can be more than one test case enib.
What does your code output for:
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.
What does your code output for:
Code: Select all
1 10
100 200
help WA! 100
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;
}
#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;
}
Code: Select all
Helppp Why? WA
I don't know why it wrong here is my cod plzz help
[/code]
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;
}
100 RE help me???
why my code get runtime error, it works well in my computer
can anyone help me???
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;
}
-
- New poster
- Posts: 1
- Joined: Sat Apr 29, 2006 9:32 am
100 run time err
hello,
why i am getting run time err
any hint
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
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
100 - someone plz reply :(
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
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
100 Runtime error
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;
}
Why the runtime error ? I mean I tested for 1000000 and it was quite fast... =/
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact: