Page 83 of 93

Re: 100 The 3n+1 Problem - WA

Posted: Sat Jun 18, 2011 12:02 pm
by zobayer
what is your output for

Code: Select all

1 1
It should be

Code: Select all

1 1 1
And to check EOF, this is better, because, sometimes, file may contain trailing garbage

Code: Select all

while(cin >> x >> y) {
}
or
while(scanf("%d %d", &x, &y)==2) {
}
Output format is ok I think.

AND BEFORE OPENING NEW THREADS, SEARCH THE FORUM FIRST FOR EXISTING THREADS, AND IF POSSIBLE, USE THOSE.

Re: 100 Time Limit Exceeded

Posted: Wed Jun 22, 2011 12:11 pm
by thomgil
Java is slower than C++
in most cases that doesn't matter

lite

Posted: Sat Jun 25, 2011 12:11 am
by thelite
I am not sure coz i am new in this forum as you may notice :)

Re: If you get WA in problem 100, read me before post!

Posted: Thu Jun 30, 2011 6:57 pm
by nt90200
I write it in C.
Can anyone help me with this?
WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int howlong(int q)
{    int a;
    a=0;  
    qwe:
    if(q==1)
    {goto asd;
            }
    else if(q%2==1)
    {q=3*q+1;
         }
    else
    {q=q/2;
        }
    a++;
    
    goto qwe;
    asd:
    a++;

    return a; 
     
}
main()
{
    int a,b,c,i,j;
    
    
    scanf("%d %d",&i,&j);
      if(i>j)
     {c=i;
      i=j;
      j=c;      
               
    
    
    
    a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      c=i;
      i=j;
      j=c;
    printf("%d %d %d",i,j,a);
    }

  else{
  a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      printf("%d %d %d",i,j,a); 
       }  
    return 0;
}

Re: If you get WA in problem 100, read me before post!

Posted: Fri Jul 01, 2011 9:17 am
by thefourtheye
nt90200 wrote:I write it in C.
Can anyone help me with this?
WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int howlong(int q)
{    int a;
    a=0;  
    qwe:
    if(q==1)
    {goto asd;
            }
    else if(q%2==1)
    {q=3*q+1;
         }
    else
    {q=q/2;
        }
    a++;
    
    goto qwe;
    asd:
    a++;

    return a; 
     
}
main()
{
    int a,b,c,i,j;
    
    
    scanf("%d %d",&i,&j);
      if(i>j)
     {c=i;
      i=j;
      j=c;      
               
    
    
    
    a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      c=i;
      i=j;
      j=c;
    printf("%d %d %d",i,j,a);
    }

  else{
  a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      printf("%d %d %d",i,j,a); 
       }  
    return 0;
}
Try printing like this

printf("%d %d %d\n",i,j,a);

Re: If you get WA in problem 100, read me before post!

Posted: Sat Jul 02, 2011 5:12 am
by nt90200
thefourtheye wrote:
nt90200 wrote:I write it in C.
Can anyone help me with this?
WA

Code: Select all

#include<stdio.h>
#include<stdlib.h>
int howlong(int q)
{    int a;
    a=0;  
    qwe:
    if(q==1)
    {goto asd;
            }
    else if(q%2==1)
    {q=3*q+1;
         }
    else
    {q=q/2;
        }
    a++;
    
    goto qwe;
    asd:
    a++;

    return a; 
     
}
main()
{
    int a,b,c,i,j;
    
    
    scanf("%d %d",&i,&j);
      if(i>j)
     {c=i;
      i=j;
      j=c;      
               
    
    
    
    a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      c=i;
      i=j;
      j=c;
    printf("%d %d %d",i,j,a);
    }

  else{
  a=howlong(i);
    b=i+1;
    for(b;b<j+1;b++)
    {
    if(howlong(b)>a)                
     {a=howlong(b);
     }               
    }
      printf("%d %d %d",i,j,a); 
       }  
    return 0;
}
Try printing like this

printf("%d %d %d\n",i,j,a);

It still WA

Re: If you get WA in problem 100, read me before post!

Posted: Wed Sep 21, 2011 4:46 pm
by GOLDENMASTER
i got WA can anyone help me out
[//#include<conio.h>
#include<iostream>
int table[10000000]={0};
int count(unsigned int);
main()
{
int s[1000],e[1000],max[1000]={1},j;
unsigned int i=0;
while(!std::cin.eof())
{
//int i=0;
std::cin>>s>>e;
j=i;
i++;}
for(int k=0;k<j;k++)
{
for(int i=s[k];i<=e[k];i++)
{
count(i);
}
for(int i=s[k];i<=e[k];i++)
{
if(max[k]<table)
{
max[k]=table;
}
}
//string ss=(string)s;
std::cout<<s[k]<<" "<<e[k]<<" "<<max[k]<<"\n";
}
//getch();
}
int count(unsigned int i)
{if(i<=10000000)
{
if(table==0)
{
if(i%2==0)
{
table=count(i/2)+1;
}
else if(i==1)
{
return 1;
}
else
{
table=count((3*i)+1)+1;
}
}
return table;
}else
{
if (i%2==0)
return(count(i/2)+1);
else
return(count((3*i)+1)+1);}
}
]

3n + 1

Posted: Thu Oct 13, 2011 12:54 am
by tuxskywalker
Rather a newbie at ANSI C. :oops:

I submitted this code for 3n+1 problem and get WA. :cry: Any suggestions?

#include <stdio.h>

int cycle(int n)
{
int count = 1;
while(n != 1)
{
if(n%2==0){ n /= 2; }
else{ n = (3*n)+1; }
++count;
}
return count;
}


int main()
{

int i,a,b;
scanf("%d %d", &a, &b);
while(!feof(stdin))
{
int test, result = 0;
for(i=a; i<=b; ++i)
{
test = cycle(i);
if( test > result){ result = test; }
}
printf("%d %d %d\n", a, b, result);
scanf("%d %d", &a, &b);
}
return 0;

}

Re: If you get WA in problem 100, read me before post!

Posted: Sat Oct 15, 2011 7:51 am
by zachaakelly
I've been tinkering with this for a while, and I keep getting WA with almost no idea why

Code: Select all

[#include <stdio.h>

unsigned int i, j, num1, count, count1;
unsigned int backw = 0;
unsigned long num2;

unsigned long nextnum(int n){
    if(n%2==0){
        return n/2;
    } else{
        return 3*n+1;
    }
}

int main(void) {
    while(scanf("%d %d", &i, &j)==2){;
    if(i>j){
        num1 = i;
        i = j;
        j = num1;
        backw = 1;
    }
    for(num1=i;num1<=j;num1++){
        num2 = (long)num1;
        count = 1;
        while(num2!=1){
            num2=nextnum(num2);
            count++;
        }
        if (count>count1){
            count1 = count;
        }
    }
    if(!backw){
        printf("%d %d %d\n",i,j,count1);
    } else {
        printf("%d %d %d\n",j,i,count1);
    }
    count1 = 0;
    }
    return 0;
}
/code]

100 java runtime error

Posted: Fri Dec 16, 2011 5:33 am
by qiusha
First time using Uva, got serious run time error problem, can anyone help, thanks.

import java.io.*;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
class Main{
static long max =0;
public static void three(long x, long y){
long i = 0;
long k =0;
for(long j=y;j<=x;j++){
k=j;
i++;
while(k!=1){
if(k%2==0){
k=k/2;
i++;
}
else{
k=3*k+1;
i++;
}
}
if (i>max){
max=i;
}
i=0;
}
}
public static void main(String args[]){
String thisLine;
ArrayList<String> al = new ArrayList<String>();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((thisLine = br.readLine()).length() != 0){
StringTokenizer st = new StringTokenizer(thisLine);
if(st.hasMoreTokens()){
String a = st.nextToken();
String b =st.nextToken();
al.add(a);
al.add(b);
}
}
for(int i=0; i<al.size()-1; i=i+2){
long y = Long.parseLong(al.get(i));
long x = Long.parseLong(al.get(i+1));
if(x>=y){
three(x,y);
if(i==al.size()-2){
System.out.print(y+" "+x+" "+max);
}
else{
System.out.println(y+" "+x+" "+max);
}
max=0;
}
else{
three(y,x);
if(i==al.size()-2){
System.out.print(y+" "+x+" "+max);
}
else{
System.out.println(y+" "+x+" "+max);
}
max=0;
}
}
System.exit(0);
}catch(IOException e){
System.exit(1);
}
}
}

Re: If you get WA in problem 100, read me before post!

Posted: Sun Dec 25, 2011 6:47 pm
by javacom
Could you please tell me why I get WY with this code ? :(

Code: Select all

#include <iostream>
#include <algorithm>


using namespace std;
unsigned long int calc(unsigned long int val){
	int counter=1;
	while(val!=1){
		if(val%2==0){
			val=val/2;
			counter++;
		}
		else{
			val=val * 3 +1;
			counter++;
		}
	}
	return counter;
}
int main() {
	unsigned long int start, startCount, end, endCount, maxCount;

	while(cin>> start >> end){
	startCount=start;
	endCount = end;
	unsigned long int maxNum=max(start, end);
	unsigned long int minNum=min(start, end);
	maxCount = calc(minNum);
	while(minNum != maxNum){
		maxCount = max(maxCount, calc(minNum));
		++minNum;
	}
	cout << startCount << " " << endCount << " " << maxCount << endl;
	}
	return 0;
}

Please Help, why I am getting WA (100) c++ ?

Posted: Sun Dec 25, 2011 7:12 pm
by javacom
could you please tell me why I'm getting WY (100 3n+1 problem) with this code ?

Code: Select all

#include <iostream>
#include <algorithm>


using namespace std;
unsigned long int calc(unsigned long int val){
   int counter=1;
   while(val!=1){
      if(val%2==0){
         val=val/2;
         counter++;
      }
      else{
         val=val * 3 +1;
         counter++;
      }
   }
   return counter;
}
int main() {
   unsigned long int start, startCount, end, endCount, maxCount;

   while(cin>> start >> end){
   startCount=start;
   endCount = end;
   unsigned long int maxNum=max(start, end);
   unsigned long int minNum=min(start, end);
   maxCount = calc(minNum);
   while(minNum != maxNum){
      maxCount = max(maxCount, calc(minNum));
      ++minNum;
   }
   cout << startCount << " " << endCount << " " << maxCount << endl;
   }
   return 0;
}

Re: If you get WA in problem 100, read me before post!

Posted: Sun Jan 01, 2012 3:17 pm
by helloneo
minNum != maxNum

should be

minNum <= maxNum

Re: If you get WA in problem 100, read me before post!

Posted: Thu Jan 05, 2012 4:34 pm
by javacom
helloneo wrote:minNum != maxNum

should be

minNum <= maxNum
Thank you, it works. AC finally :)

Re: 100 java runtime error

Posted: Fri Jan 06, 2012 10:56 pm
by brianfry713
change this line from:
while((thisLine = br.readLine()).length() != 0){
to this:
while((thisLine = br.readLine()) != null){

Trying to take the length of a null String throws a NullPointerException.

Also change to a println every time and you'll get AC.

Paste your code inside code blocks and use the existing thread for a problem.