1 / 2
Dec 2012

problem http://www.spoj.com/problems/ACODE/1
My C++ code got accepted, i tried to convert C++ to java. It runned OK on my computer but it got NZEC error on SPOJ. Can someone help me fix this java code?!!!
C++ code

#include<cstdio>
#include<string.h>
int a[5002];
char x[5002];
int main(){
    a[0]=1;
    a[1]=1;
    int l,temp;
    char num[5001];
    while(true){
    scanf("%s",&num);
    if(num[0]=='0')
        break;
    x[0]='0';
    x[1]='\0';
    strcat(x,num);
    l=strlen(x)-1;
    for(int i=2;i<=l;i++){
        temp=(x[i-1]-'0')*10+x[i]-'0';
        if(temp==10 || temp==20)
            a[i]=a[i-2];
        else if(temp<10)
            a[i]=a[i-1];
        else if(temp<=26)
            a[i]=a[i-1]+a[i-2];
        else
            a[i]=a[i-1];
    }
    printf("%d\n",a[l]);
    }
}

My JAVA CODE:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
    /**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
	BufferedWriter w=new BufferedWriter(new OutputStreamWriter(System.out));
	int []a=new int[5002];
	StringBuilder x=null;
    a[0]=1;
    a[1]=1;
    int l,temp;
    String num;
    while(true){
    	num=r.readLine();
        if(num.charAt(0)=='0')
            break;
        x=new StringBuilder();
        x.insert(0, '0');
        x.insert(1, num);
        l=x.length()-1;
        for(int i=2;i<=l;i++){
            temp=(x.charAt(i-1)-'0')*10+x.charAt(i)-'0';
            if(temp==10 || temp==20)
                a[i]=a[i-2];
            else if(temp<10)
                a[i]=a[i-1];
            else if(temp<=26)
                a[i]=a[i-1]+a[i-2];
            else
                a[i]=a[i-1];
        }
        w.append(String.valueOf(a[l])+"\n");
    }
    r.close();
	w.close();
}
}
  • created

    Dec '12
  • last reply

    Sep '18
  • 1

    reply

  • 884

    views

  • 2

    users

  • 1

    link

5 years later