Page 41 of 93

Posted: Wed Sep 21, 2005 8:16 am
by chunyi81
Try this input:

1 3

You code gives:

1 3 2

But the correct output should be

1 3 8

Ops!

Posted: Wed Sep 21, 2005 10:44 pm
by patrickriva
Thank you very much I'll have a look at it!

Cheers

100 3n+1 problem

Posted: Sat Sep 24, 2005 11:45 am
by Artikali
i solve this problem, judge accepted
but if i enter from 1 to 1000000 result will be after 2-3 minutes
has every one solve this problem which results will be in 1-2 seconds
for long intervals

Re: 100 3n+1 problem

Posted: Sat Sep 24, 2005 10:18 pm
by Martin Macko
Artikali wrote:i solve this problem, judge accepted
but if i enter from 1 to 1000000 result will be after 2-3 minutes
has every one solve this problem which results will be in 1-2 seconds
for long intervals
Read threads on this problem in the Volume I forum.

3n+1 problem

Posted: Mon Sep 26, 2005 8:51 pm
by mbd01
Hi, I am absolutely new here. I wrote a program for problem#100 and the error message is "Wrong Answer"
The program is here........

#include<stdio.h>
main()
{
long int n,x,y;
int i,max;

while(fscanf(stdin,"%ld%ld",&x,&y)!=EOF)
{
printf("%ld %ld ",x,y);
if(y<x) { n=x;x=y;y=n;}
max=0;
for(;x<=y;x++)
{

n=x;
i=1;
do{
// printf("%ld ",n);
if(n%2==0)
n=n/2;
else
n=3*n+1;
i=i+1;
}while(n!=1);

if(max<i) max=i;

}
printf("%d\n",max);
}
}

Can anyone tell me for which input the answer is wrong?

Posted: Tue Sep 27, 2005 8:15 am
by Dominik Michniewski
Search on board and first read other threads connected with problem 100, and after that post new one.

Best regards
DM

Problem with input

Posted: Tue Sep 27, 2005 9:40 am
by WDWD
Hi

I have some problems with handling the input in 100. I dont know how or in which format I will receive it...
I use C++.

Thanks

Re: Problem with input

Posted: Tue Sep 27, 2005 9:49 am
by Martin Macko
WDWD wrote:Hi

I have some problems with handling the input in 100. I dont know how or in which format I will receive it...
I use C++.

Thanks
There are two numbers (i and j) on each line in the input, which is terminated by EOF.
You can write something like this:

Code: Select all

int i, j;
while (cin>>i>>j) cout<<i<<" "<<j<<" "<<solve(i,j)<<endl;
return 0;
But mind that cin>> and cout<< are rather slow...

Posted: Wed Sep 28, 2005 2:22 am
by Kamik
hello, can anyone help me?
i got WA, but i tested with multiple inputs, i dont know why dont accept.

Code: Select all

#include <iostream>
#include <sstream>
#include <string>

int main(int argc, char *argv[])
{
	unsigned long int aux, aux2, inicio, fim, o_inicio, o_fim;
	char linha[20];
	int maximo, atual;
	std::istringstream *buffer;
	
	do
	{
		std::cin.getline(linha, 19);
		if(strcmp(linha,"") != 0)
		{
			buffer = new std::istringstream;
			buffer->str(linha);
			*buffer >> o_inicio >> o_fim;

			if(o_inicio > o_fim)
			{
				inicio = o_fim;
				fim = o_inicio;
			}
			else
			{
				inicio = o_inicio;
				fim = o_fim;
			}
			maximo = 0;
			for(aux = inicio; aux <= fim; aux++)
			{
				atual = 1;
				aux2 = aux;
				if(aux * 2 <= fim)
					continue;
				while(aux2 != 1)
				{
					if(aux2 % 2 != 0)
						aux2 = 3 * aux2 + 1;
					else
						aux2 = aux2 / 2;
					atual++;
				}
				if(atual > maximo)
					maximo = atual;
			}
			delete buffer;
			std::cout << o_inicio << " " << o_fim << " " << maximo << "\n";
		}
	} while(strcmp(linha,"") != 0);
	return 0;
}


100 java - WA...

Posted: Sun Oct 09, 2005 8:22 pm
by CGR
This pb is driven me crazy..
WA, and I don't know why. I read all the topics but didn't find anything..

For this input :

1 1
1 2
2 1
3 3
3627 5749

I obtain :

1 1 1
1 2 2
2 1 2
3 3 8
3627 5749 238

This is my code :

import java.util.*;
import java.io.*;

class Main {

public static void main(String[] args) {

StringTokenizer st;
String s;
int i,j;

while ((s = readLn(50).trim()) != null) {
st= new StringTokenizer(s);
i= Integer.parseInt(st.nextToken());
j= Integer.parseInt(st.nextToken());
System.out.println(i+" "+j+" "+maiorCompCiclo(i,j));
}
}

static int compCiclo (int n) {
int cont = 1;

while (n!=1) {
if (n%2 == 0) n=n/2;
else n=(3*n)+1;
cont ++;
}

return cont;
}

static int maiorCompCiclo(int i, int j) {
int max=0, comp, menor, maior;

if (i<j) {menor=i; maior=j;}
else {menor=j; maior=i;}

for (int k=menor;k<maior+1;k++) {
comp = compCiclo(k);
if (comp>max) max = comp;
}

return max;
}

static String readLn (int maxLg) { ... }
}


Can anyone help me ?

Re: 100 java - WA...

Posted: Tue Oct 11, 2005 3:38 pm
by tan_Yui
Hi, CGR.
Could you write your code fully?
Although I cannot satisfactorily read the Java code, I can check by using i/o with my Accepted code.
static String readLn (int maxLg) { ... }
Best regards.

Posted: Wed Oct 12, 2005 7:30 pm
by CGR
import java.util.*;
import java.io.*;

class Main {

public static void main(String[] args) {

StringTokenizer st;
String s;
int i,j;

while ((s = readLn(100).trim()) != null) {
st= new StringTokenizer(s);
i= Integer.parseInt(st.nextToken());
j= Integer.parseInt(st.nextToken());
System.out.println(i+" "+j+" "+maiorCompCiclo(i,j));
}
}

static int compCiclo (int n) {
int cont = 1;

while (n!=1) {
if (n%2 == 0) n=n/2;
else n=(3*n)+1;
cont ++;
}

return cont;
}

static int maiorCompCiclo(int i, int j) {
int max=0, comp, menor, maior;

if (i<j) {menor=i; maior=j;}
else {menor=j; maior=i;}

for (int k=menor;k<maior+1;k++) {
comp = compCiclo(k);
if (comp>max) max = comp;
}

return max;
}

static String readLn (int maxLg) {

byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try {
while (lg < maxLg) {
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e) { return (null); }

if ((car < 0) && (lg == 0)) return (null);
return (new String (lin, 0, lg-1));
}
}

Posted: Thu Oct 13, 2005 8:56 am
by tan_Yui
I executed your code by using the I/O which I prepared.
Your code outputs incorrect answer for them.
Following is the I/O.
By the way, it should be efficient code because the limit of input number is huge, 1 million.
Brute force method will cause TimeLimitExceeded.

Input :
1 10
100 200
201 210
900 1000
1 999999
837799 837799
837800 999999
511936 837798
96 97
96 98
97 97
97 98
5000 500
312 232
13255 3711
200000 100000
1 1
2 2
2 3
Output should be :
1 10 20
100 200 125
201 210 89
900 1000 174
1 999999 525
837799 837799 525
837800 999999 476
511936 837798 468
96 97 119
96 98 119
97 97 119
97 98 119
5000 500 238
312 232 128
13255 3711 276
200000 100000 383
1 1 1
2 2 2
2 3 8
Best regards.

Compile error and general

Posted: Wed Oct 19, 2005 3:00 pm
by TheLordFerada
Hiho

I've got a compile error with the code at http://home.arcor.de/thelordferada/v1_100.c, but it compiles without any errors at my pc (gcc 3.3.3, athlon 1600+); can you help me :-?

also is there a possibility to get the error-messages from the compiler after submission ?

thx TLF[/url]

Posted: Wed Oct 19, 2005 4:04 pm
by shamim
you could get the compile error message from the compiler by maill, if you enabled that option, when registering.