Quantcast
Channel: Online Judge System - SPOJ Discussion board
Viewing all 2136 articles
Browse latest View live

PRIME_T in RUBY

$
0
0

@kopar1 wrote:

def PRIME_T(n)
	if n<2
	    return printf "NIE"
	else
	    for i in 2..(n-1)
	        if n%i==0
	            return printf "NIE"
	        end
	    end
	    return printf "TAK"
	end
end

On my computer works perfectly?
So whats wrong?

Posts: 4

Participants: 2

Read full topic


Life, Universe and everything else

$
0
0

@vmussato wrote:

Hello friends,

Im new to spoj and im trying to figure why my code is wrong, i searched the forum but nothing matched my problem.

It works on my compiler, but all i get from the online judge is wrong code.

Could you guys help me with this problem?

			#include <stdio.h>
			
			int main(){
				
				int input[5];
				int output[5];
				int i, j;
				
				for(i = 0; i < 5; i++){
					scanf("%i", &input[i]);
				}
				printf("\n");
				for(j = 0; j < 5;j++){
					
					if(input[j]==42){
						printf("%d\n", input[j]	);
						break;
					} else{
						printf("%d\n", input[j]);
					}
						
				}
				return 0;
			}

Posts: 6

Participants: 4

Read full topic

PALIN (java) time limit exceed

$
0
0

@godzila555 wrote:

Hello guys, could you help me to improve the code efficiency? i always get "time limit exceeded".

here is my solution:

import java.util.*;
import java.lang.*;
import java.math.*;

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0; i System.out.println(findpalindrome(sc.next()));
}
}

private static String findpalindrome(String n){
	if(n.length()==1){
		if(n.charAt(0)-'0'<9)return ""+(n.charAt(0)-'0'+1);
		else return "11";
	}
	String result="";
		if(firstpartlarger(n)){
			if(n.length()%2==0)result=n.substring(0, n.length()/2)+reverse(n.substring(0, n.length()/2));
			else result=n.substring(0, n.length()/2)+n.charAt(n.length()/2)+reverse(n.substring(0, n.length()/2));
		}
		else{
			if(n.length()%2==0){
			String length=n.substring(0, n.length()/2);
			result=increase(length);
			if(length.length()<result.length()){
				result+=reverse(result.substring(0, result.length()-1));
			}
			else{
				result+=reverse(result);
			}
			}
			else{
			result=increase(n.substring(0, n.length()/2+1));
			result+=reverse(result.substring(0, n.length()/2));
			}
		}
		return result;
	}


	private static String increase(String n){
		BigInteger i=new BigInteger(n);
		i=i.add(BigInteger.ONE);
		return i.toString();
	}


private static String reverse(String n){
	StringBuilder builder=new StringBuilder(n);
	builder.reverse();
	return builder+"";
}

private static boolean firstpartlarger(String n){
	return reverse(n.substring(0,  n.length()/2)).compareTo(n.substring(n.length()-n.length()/2))>0;
}

}

Posts: 1

Participants: 1

Read full topic

Source code 16373935.runtime error(NZEC)

$
0
0

@vaibhavg_2896 wrote:

I solved CPTTRN1 - Character Patterns (Act 1) Q. of basic problems I did using python 3.4 ,
The expected output and my code output are exactly the same then also your "Online judge system "
is giving RE-(NZEC).
kindly correct it.
My source code is 16373935.

Posts: 1

Participants: 1

Read full topic

WA in ACS (Concrete Simulation)

FASTFLOW - TLE - Help with improvements to implementation

$
0
0

@mutant wrote:

Hi all,

I have tried two approaches for FASTFLOW problem. Both of them are failing with TLE. I have searched for solutions online and found couple of them using BFS approach. I am doing the same and implementation seems almost similar. Could someone tell me where I can improve my solutions?

http://pastebin.com/QrGiKMET - O(V^3) implementation using Relabel to front algorithm
http://pastebin.com/fvWuBGXT - Using BFS approach

Posts: 1

Participants: 1

Read full topic

STRHH problem

$
0
0

@aastik wrote:

import java.util.Scanner;
public class Main{
	public static void main(String args[]){
		Scanner input2 = new Scanner(System.in);
		Scanner input = new Scanner(System.in);
		int t = input2.nextInt();
		for(int i=0;i<t;i++){
			String s = input.nextLine();
			char[] carr = s.toCharArray();
			for(int j=0;j<carr.length/2;j=j+2){
				System.out.print(carr[j]);
			}
			System.out.println();
		}
	}
}

My code is working fine in Eclipse but giving a NZEC error in SPOJ. Can anyone point out why?

Posts: 6

Participants: 2

Read full topic

WA in cpttrn1

$
0
0

@oceanofdreams wrote:

I used C++ to solve this problem and I get the same expected output. However, I am getting "wrong answer". Can anyone please help me to identify the problem below?

    #include <stdio.h>
 
int main(void) {
	// your code here
	int n, l, c, num = 0;
	int i,j;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d %d", &l, &c);
		num = 0;
		for(i = 0; i < l; i++)
        {
            for(j = 0; j < c ; j++)
            {
                if((num%2)==0)
                {
                    printf("*");
                }
                else
                {
                    printf(".");
                }
                num ++;
            }
            printf("\n");
        }
        printf("\n");
	}
	return 0;
}

Posts: 3

Participants: 2

Read full topic


Divisor summation problem

$
0
0

@aastik wrote:

My code is exceeding the time limit.

import java.util.Scanner;
class spojdivisorsum {
	public static void main(String args[]){
		Scanner input = new Scanner(System.in);
		int t = input.nextInt();
		while(t>0){
			int x = input.nextInt();
			printsum(x);
			t--;
		}
	}
	public static void printsum(int x){
		int sum = 0;
		for(int i=1;i<=x/2;i++){
			if(x%i==0){
				sum=sum+i;
			}
		}
		System.out.println(sum);
	}
}

Can anyone point out a way to do better?

Posts: 2

Participants: 2

Read full topic

ARITH-Wrong Answer

$
0
0

@godzila555 wrote:

just solved the problem by replacing '\0' chars by ' '. however, the result seems the same in both cases. surprisingly it worked.

Posts: 6

Participants: 2

Read full topic

Time Limit exceeded in Prime Generator

$
0
0

@aritra741 wrote:

Hello. SPOJ keeps showing the message,"Time Limit Exceeded" when I submit my solution to The problem "Prime Generator". Would anyone please advise me on how I can optimize my code?

#include <stdio.h>
#include <math.h>

int main()
{
    int tst;
    int fir,sec,div;
    scanf("%d", &tst);
    while(tst--)
{ 
    scanf("%d %d", &fir,&sec);
    if(fir==2 || sec==2)
        printf("2\n");
    for(;fir<=sec;fir++)
    {
        if(fir%2==0)
        continue;
        for(div=2;div<=sqrt(fir);div++)
            {
                if(fir%div==0)
                    break;

            }
            if(fir%div!=0)
                printf("%d\n", fir);
    }

}
return 0;

}

Posts: 5

Participants: 2

Read full topic

The Next Palindrome Solution ID 16420937

$
0
0

@shubhu1596 wrote:

I am not able to find the problem with this code. Test this for many input seems to be working fine on ideone
but when I submit it SPOJ says WA.
Here's the code in c++(g++ 5.1)

include

include

using namespace std;

int main() {

int n=0,p=-1;
cin>>n;
long arr[n],m=0,i;

for(int q=0;q cin>>arr[q];
long a=arr[q]+1;
i=a;

	int k=p;
	i=a;
	while(i){
				m=0;
				p=-1;
				a=i;
				while(a){
						a=a/10;
						p=p+1;

						}
				a=i;


			while(a){
				m=m+(a%10)*pow(10,p);
				a=a/10;
				p=p-1;

				}

			if(i==m)
			{cout<<m<<"\n";
			i=0;break;
			}
			else
			i=i+1;
			}
	}	// your code here

return 0;

}

Posts: 1

Participants: 1

Read full topic

The Cursed Room - Why is my answer wrong?

$
0
0

@waqqas wrote:

I've implemented a solution to The Cursed Room problem.

I ran the solution on command-line and on ideone.com and in both cases, my application's output matches the expected output listed in the question.

When I submit my solution to SPOJ, it claims that the answer is wrong. I do not understand why it is so. Is there a way I can find out in which scenario my code has failed?

Also, I'm new to SPOJ so my apologies if I'm posting this question in the wrong place. Thanks.

Posts: 1

Participants: 1

Read full topic

CCPTRN 5 what's wrong?

$
0
0

@godzila555 wrote:

Hello, i can't pass character pattern (act 5) . it tells me wrong answer. But i checked my code on some cases and it produced correct resuts. could you tell me what's wrong with it?

here is the task: http://www.spoj.com/problems/CPTTRN5/

here is my solution:

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		for(int i=0; i<t; ++i){
			int line=sc.nextInt();
			int column=sc.nextInt();
			int height=sc.nextInt();
			for(int j=0; j<line*height+line+1; ++j){
				for(int k=0; k<column*height+column+1; ++k){
					if(j%(height+1)==0 || k%(height+1)==0)System.out.print("*");
					else if((k/(height+1)+j/(height+1))%2==0){
						if(k%(height+1)==j%(height+1)){
							System.out.print("\\");
						}
						else if((k%(height+1)+j%(height+1))%(height+1)==0){
							System.out.print(".");
						}
						else System.out.print(" ");
					}
					else if((k/(height+1)+j/(height+1))%2==1){
						if((k%(height+1)+j%(height+1))%(height+1)==0){
							System.out.print("/");
						}
						else if(k%(height+1)==j%(height+1)){
							System.out.print(".");
						}
						else System.out.print(" ");
					}
					
				}
				System.out.println();
			}
			if(i!=t-1)System.out.println();
		}
		sc.close();
	}
}

Posts: 6

Participants: 2

Read full topic

Ccptrn 7

$
0
0

@godzila555 wrote:

Hello guys, i have problem with the task: http://www.spoj.com/problems/CPTTRN7/
i always get TLE, but could you tell me what is the main reason of long running time of my code?
I'm a beginner, so i really don't know what to do with it.
Here is the solution:

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		for(int i=0; i<t; ++i){
			int r=sc.nextInt();
			int c=sc.nextInt();
			int s=sc.nextInt();
			for(int j=0; j<r; ++j){
				for(int n=0; n<2*s; ++n){
					for(int k=0; k<c; ++k){
						for(int m=0; m<2*s; ++m){
							if(n<s){
								if(m==s-n-1){
									System.out.print("/");
								}
								else if(m==s+n){
									System.out.print("\\");
								}
								else{
									System.out.print(".");
								}
							}
							else{
								if(m==n-s){
									System.out.print("\\");
								}
								else if(3*s-1-n==m){
									System.out.print("/");
								}
								else{
									System.out.print(".");
								}
							}
						}
						
				  	}
				  	System.out.println();
				}
			}
			if(i!=t-1)System.out.println();
		}
	}
}

Posts: 2

Participants: 2

Read full topic


PALIN - The Next Palindrome

$
0
0

@shubhu1596 wrote:

Solution Id:16444505
Even multiple test cases are successful at ideone spoj is showing WA.
Please suggest the problem with this code.
Thanks

Posts: 2

Participants: 2

Read full topic

CPTTRN5 - Character Patterns (Act 5)

CPTTRN6 - Character Patterns (Act 6)

$
0
0

@adi_tdkr wrote:

What is wrong in my code because I am getting wrong answer on spoj?
int main()
{
int t;
scanf("%d",&t);
while(t--){
int l,c,h,w,i,j,k,p,q,x,y,z;
scanf("%d%d%d%d",&l,&c,&h,&w);
if(c==1 && w==1 ){
for(x=1;x<=l*h+1;x++){
for(y=1;y<=w;y++){
if(x%2==0){ printf("-+-\n"); }
else{
for(i=1;i<=h;i++){
for(j=1;j<=h+1;j++){
if(j%2==0){ printf("|");}
else { printf(".");}
} printf("\n");
}}}
}}
else if(h==1){
for(i=0;i<1;i++){
for(j=1;j<=(c*w)+1;j++){
if(j%2==0){ printf("|"); }
else { printf(".."); }
}
} printf("\n");
for(i=1;i<=l*2;i++){
if(i%2==0){
for(k=0;k<1;k++){
for(z=1;z<=(c*w)+1;z++){
if(z%2==0){ printf("|"); }
else{ printf(".."); }
} printf("\n");
}
}else{
for(k=0;k<1;k++){
for(z=1;z<=(c*w)+1;z++){
if(z%2==0){ printf("+"); }
else{ printf("--"); }
}
printf("\n");
}
}
}
}
else{
for(x=1;x<=l*2+1;x++){
if(x%2==0){
for(i=0;i<1;i++){
for(j=1;j<=c*w+1;j++){
if(j%2==0){ printf("+");}
else{ printf("--");}
}
} printf("\n");
}else{
for(i=0;i for(j=1;j<=c*w+1;j++){
if(j%2==0) { printf("|"); }
else{ printf(".."); }
}
printf("\n"); }}

}
}

}
return 0;

}

Posts: 5

Participants: 2

Read full topic

PALIN problem: requesting help diagnosing NZEC in my Python solution

$
0
0

@luoji83 wrote:

Hey, in the "Read this before posting" I checked:

  • input/output methods - am using raw_input()
  • test your code on boundary cases - generated test cases from 0 to inputs up to 100000 digits
  • search - the few results didn't contain helpful answers

Other things pertinent info that has left me stumped:
- chose "ideone it" on the fail results page, and it runs fine there
- ensured that "python 2.7" was correct to choose from the language dropdown, by comparing to output of "$python --version"
- have solved another challenge (onp) with python without issue
- the fail results page says that the time spent is "0.00" meaning that it's failing very very early

Ok, here's the code, any help appreciated:

#!/usr/bin/env python

# http://www.spoj.com/problems/PALIN/
# PALIN - The Next Palindrome

import sys

class PalindromeIterator:
    def __init__(self, n):
        self.setAt(n)

    def setAt(self, n):
        # precondition input

        log = 0
        s = str(n)
        if len(s) % 2:
            # odd
            log = len(s)/2
            self.count = int(s[0:log+1])
            self.pivot = True
        else:
            # even
            log = len(s)/2-1
            self.count = int(s[0:log+1])
            self.pivot = False

        self.limitLo = 10**log
        self.limitHi = 10**(log+1) - 1

        # cheap way to initialize
        while self.current() < n:
            self.next()

        #self.status()

    def status(self):
        print "  count: %d" % self.count
        print "  pivot: %d" % self.pivot
        print "limitLo: %d" % self.limitLo
        print "limitHi: %d" % self.limitHi
        print '--'

    def current(self):
        s = str(self.count)
        if self.pivot:
            return int(s + (s[:-1])[::-1])
        else:
            return int(s + s[::-1])

    def __iter__(self):
        return self

    def next(self):
        answer = self.current()

        # where next?
        self.count += 1

        if self.count > self.limitHi:
            if self.pivot:
                # just reset, unset pivot
                self.count = self.limitLo
                self.pivot = False
            else:
                # set new limits
                self.limitLo = self.LimitLo * 10
                self.count = self.LimitLo * 10 - 1
                self.pivot = True

        #self.status()

        # done
        return int(answer)

n = int(raw_input())

for z in range(n):
    k = 0
    try:
        k = int(raw_input())
    except:
        break
    
    it = iter(PalindromeIterator(k))
    while it.current() <= k:
        it.next()

    print it.current()

sys.exit(0)

Thanks!

Posts: 1

Participants: 1

Read full topic

Life, the Universe, and Everything .c

$
0
0

@lmoreno wrote:

why is it not working in the test?

#include <stdio.h>
    void main()
    {int *a,h;
    a=malloc(4);
    int b=0,x=1;
    while (b!=42)
    {scanf("%i",&b);
    a=realloc(a,x*sizeof(int));
    a[x-1]=b;x++;
    }
    for (h=0;h<x-2;h++){printf("%i\n",a[h]);}
    }

Posts: 1

Participants: 1

Read full topic

Viewing all 2136 articles
Browse latest View live