Re: 100 - Wrong Answer!! Please help!!
Posted: Thu Apr 16, 2009 12:13 pm
No idea sorry. But try testing it out with your own numbers and see how far its off. Maybe that'll give you a clue on what's wrong.
This is wrong, because there may be leading or trailing spaces, and more than one space between the numbers.long i = Long.parseLong(str.split(" ")[0]);
long j = Long.parseLong(str.split(" ")[1]);
You don't have to catch exceptions, testing system does that for you. And when it catches exception, it gives you a more meaningful response - "runtime error", instead of "wrong answer".catch (Exception e){
System.exit(0);
}
Code: Select all
import java.util.Scanner;
public class Main{
public static long cycle_length(long num){
long count = 1;
while (num != 1){
if (num%2 == 0){
num = num / 2;
}
else{
num = num * 3 + 1;
}
count++;
}
return count;
}
public static void main(String args[]){
try{
Scanner scanner = new Scanner(System.in);
long i = scanner.nextLong();
long j = scanner.nextLong();
while (true){
long a;
long b;
if (i < j){
a = i;
b = j;
}
else{
a = j;
b = i;
}
long max = 0;
for (long k = a; k <= b; k++){
long temp = cycle_length(k);
if (max < temp){
max = temp;
}
}
System.out.println("" + i + " " + j + " " + max);
i = scanner.nextLong();
j = scanner.nextLong();
}
}
catch (Exception e){
System.exit(0);
}
}
}
Code: Select all
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static int cycle_length(int num){
int count = 1;
while (num != 1){
if ((num & 1) == 0){
num = num>>1;
}
else{
num = (num<<1) + num + 1;
}
count++;
}
return count;
}
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(br.readLine());
while (token.hasMoreTokens()){
int i = Integer.parseInt(token.nextToken());
int j = Integer.parseInt(token.nextToken());
int max = 0;
int a = Math.min(i, j);
int b = Math.max(i, j);
System.out.println("" + i + " " + j);
for (int k = a; k <= b; k++){
int temp = cycle_length(k);
if (max < temp){
max = temp;
}
}
System.out.println("" + i + " " + j + " " + max);
token = new StringTokenizer(br.readLine());
}
br.close();
}
}
Code: Select all
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static int cycle_length(int num){
int count = 1;
while (num != 1){
if ((num & 1) == 0){
num = num>>1;
}
else{
num = (num<<1) + num + 1;
}
count++;
}
return count;
}
public static void main(String args[]){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(br.readLine());
while (token.hasMoreTokens()){
int i = Integer.parseInt(token.nextToken());
int j = Integer.parseInt(token.nextToken());
int max = 0;
int a = Math.min(i, j);
int b = Math.max(i, j);
for (int k = a; k <= b; k++){
int temp = cycle_length(k);
if (max < temp){
max = temp;
}
}
System.out.println("" + i + " " + j + " " + max);
token = new StringTokenizer(br.readLine());
}
br.close();
}
catch(Exception e){
System.exit(0);
}
}
}
Code: Select all
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// Another variables
StringTokenizer st;
String sa, sb;
double da, db;
// Another variables
// Some of code ...
// Some of code ...
st = new StringTokenizer(in.readLine());
sa = st.nextToken(); // String
sb = st.nextToken(); // String
da = Double.parseDouble(st.nextToken()); // Double
db = Double.parseDouble(st.nextToken()); // Double
// Some of code
// Some of code ...
out.flush();
}
Code: Select all
#include <stdio.h>
#include <stack.h>
#include <list.h>
#include <string.h>
#define MAX 1000001
int main()
{
int *Vector, max, n1, n2;
Vector = new int[MAX];
memset(Vector, 0, sizeof(Vector));
n1 = n2 = 1;
while(n1 < max)
{
Vector[n1] = n2;
n2++;
n1*=2;
}
stack<int,list<int> > stack;
while(1)
{
if (scanf("%d %d", &n1, &n2) == EOF)
break;
printf("%d %d ", n1, n2);
n2++;
max = 0;
for(int i=n1; i<n2; i++)
{
int k = i, cont = Vector[i];
if (cont == 0)
{
while(k>1)
{
stack.push(k);
k = (k % 2 == 0)?(k/2):(3*k+1);
cont = Vector[k];
if (cont > 0)
{
k = 1;
}
}
while(!stack.empty())
{
Vector[stack.top()] = ++cont;
stack.pop();
}
}
max = (max<cont)?cont:max;
}
printf("%d\n", max);
}
free(Vector);
return 0;
}
Code: Select all
int *Vector, max, n1, n2;
Vector = new int[MAX];
memset(Vector, 0, sizeof(Vector));
n1 = n2 = 1;
while(n1 < max) {
...
free(Vector);
Code: Select all
// FIXED CODE
#include <stdio.h>
#include <stack.h>
#include <list.h>
#include <string.h>
#define MAX 1000001
int main()
{
int *Vector, max, n1, n2;
Vector = new int[MAX];
memset(Vector, 0, MAX*sizeof(Vector));
n1 = n2 = 1;
while(n1 < MAX)
{
Vector[n1] = n2;
n2++;
n1*=2;
}
stack<int,list<int> > stack;
while(1)
{
if (scanf("%d %d", &n1, &n2) == EOF)
break;
printf("%d %d ", n1, n2);
n2++;
max = 0;
for(int i=n1; i<n2; i++)
{
int k = i, cont = Vector[i];
if (cont == 0)
{
while(k>1)
{
stack.push(k);
k = (k % 2 == 0)?(k/2):(3*k+1);
cont = Vector[k];
if (cont > 0)
{
k = 1;
}
}
while(!stack.empty())
{
Vector[stack.top()] = ++cont;
stack.pop();
}
}
max = (max<cont)?cont:max;
}
printf("%d\n", max);
}
delete Vector;
return 0;
}
Code: Select all
#include <vector.h>
int main()
{
int max, n1, n2;
vector<int> myVector(MAX);
max = MAX;
n1 = n2 = 1;
while(n1 < max)
{
myVector[n1] = n2;
n2++;
n1*=2;
}
stack<int,list<int> > stack;
while(1)
{
if (scanf("%d %d", &n1, &n2) == EOF)
break;
printf("%d %d ", n1, n2);
n2++;
max = 0;
for(int i=n1; i<n2; i++)
{
int k = i, cont = myVector[i];
if (cont == 0)
{
while(k>1)
{
stack.push(k);
k = (k % 2 == 0)?(k/2):(3*k+1);
cont = myVector[k];
if (cont > 0)
{
k = 1;
}
}
while(!stack.empty())
{
myVector[stack.top()] = ++cont;
stack.pop();
}
}
max = (max<cont)?cont:max;
}
printf("%d\n", max);
}
return 0;
}
I'm using 64-bit machine, here sizeof(Vector)==8, and sizeof(int)==4, so you pass wrong amount of memory to memset. Use memset(Vector, 0, MAX*sizeof(Vector[0])); instead.int *Vector, max, n1, n2;
Vector = new int[MAX];
memset(Vector, 0, MAX*sizeof(Vector));
Code: Select all
Vector[stack.top()] = ++cont;
You allocated memory with 'new int[MAX]', so you must use 'delete[] Vector' here.delete Vector;
Code: Select all
while(1)
{
if (scanf("%d %d", &n1, &n2) == EOF)
break;
Code: Select all
while (scanf("%d %d", &n1, &n2) == 2) {
Code: Select all
tags when posting code, so that indentation isn't lost.
[quote]but I saw that there are people with runtime of 0.000 , how is that even possible ?!?!
here is my improved solution, can you help me make it run faster ?[/quote]
I wouldn't worry about running time. This site is about improving your algorithmic and problem solving skills, and not about low level optimizations.
[quote]but I saw that there are people with runtime of 0.000 , how is that even possible ?!?![/quote]
Definetely some kind of precomputation, or maybe they've just mined judge's input.
Using a simple precomputed table with maximums on every interval of 100 numbers I've got 0.008 seconds (table takes about 36Kb, and source size limit is 40Kb, I think).