Page 1 of 1

I don't know why this got CE.

Posted: Fri Apr 07, 2006 2:03 pm
by shines
This go well on my machine, but I got CE. Why?

Code: Select all

deleted..

Posted: Fri Apr 07, 2006 5:57 pm
by Cho
It can be compiled succesfully by MinGW. But I got a strange compilation error from g++ in unix:
t.cpp:7: parse error before `+'

I then modify it and the following code can be compiled without error:

Code: Select all

#include <iostream> 
#include <string> 
#include <cstdio> 

using namespace std; 
/*
bool isupper(char x){ 
    return (x >= 'A' && x <= 'Z')? true: false; 
} 
*/
int value(char x){ 
    if(isupper(x)) 
        return x-38; 
    return x-96; 
} 

bool isprime(int n){ 
    if(n == 0 || n == 1) return true; 
    for(int i=2; i<n/2; i++){ 
        if(n % i == 0) 
            return false; 
    } 
    return true; 
} 

int main(int argc, char **argv){ 
    string word; 
    int sum; 
    while(getline(cin, word)){ 
        if(word.empty()) break; 
        sum = 0; 
        for(int i=0; i<word.size(); i++){ 
            sum += value(word[i]); 
        } 
        if(isprime(sum)) 
            cout << "It is a prime word." << endl; 
        else 
            cout << "It is not a prime word." << endl; 
    } 
    return 0; 
} 
Looks like upper() is defined even you don't include ctype.h.

Posted: Fri Apr 07, 2006 6:35 pm
by shines
I compiled with g++ 4.0 in linux. it's no problem.
but acm show this error message:
04482233_24.c:6: parse error before `)'
04482233_24.c:6: parse error before `&'

I used isupper() that I defined at another problem. It got AC. why this time it cause matter?

Posted: Fri Apr 07, 2006 9:18 pm
by mf
There's an isupper() defined in <ctype.h> and <cctype>.
Probably <iostream> caused that file to be included, and it conflicted with your version of isupper(). And you know, sometimes isupper is defined as a macro. (i.e. #define isupper(c) ...)

Re: I don't know why this got CE.

Posted: Fri Apr 07, 2006 9:35 pm
by Krzysztof Duleba
shines wrote:

Code: Select all

#include <iostream>
bool isprime(int n){
    if(n == 0 || n == 1) return true;
    for(int i=2; i<n/2; i++){
        if(n % i == 0)
            return false;
    }
    return true;
}
BTW: are you sure you want isprime() to work this way? According to it 0 and 1 are prime, so is 4. I don't think you'll get AC with that.

Posted: Sat Apr 08, 2006 12:26 am
by little joey
Yes, isupper is defined as a macro, at least in the compiler the judge uses. You can avoid compile error by first undef-ing it:

Code: Select all

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

#undef isupper
bool isupper(char x){
    return (x >= 'A' && x <= 'Z')? true: false;
}

int value(char x){
    if(isupper(x))
        return x-38;
    return x-96;
}

... etc.
and then it will compile fine.

Re: I don't know why this got CE.

Posted: Sat Apr 08, 2006 5:33 am
by Cho
Krzysztof Duleba wrote:
shines wrote:

Code: Select all

#include <iostream>
bool isprime(int n){
    if(n == 0 || n == 1) return true;
    for(int i=2; i<n/2; i++){
        if(n % i == 0)
            return false;
    }
    return true;
}
BTW: are you sure you want isprime() to work this way? According to it 0 and 1 are prime, so is 4. I don't think you'll get AC with that.
Btw, it's too slow, although it may pass for that particular problem.
By adding 3 characters and deleting 2 characters, it will be improved from O(n) to O(sqrt(n)).

Re: I don't know why this got CE.

Posted: Sat Apr 08, 2006 6:16 am
by shines
Krzysztof Duleba wrote:
shines wrote:

Code: Select all

#include <iostream>
bool isprime(int n){
    if(n == 0 || n == 1) return true;
    for(int i=2; i<n/2; i++){
        if(n % i == 0)
            return false;
    }
    return true;
}
BTW: are you sure you want isprime() to work this way? According to it 0 and 1 are prime, so is 4. I don't think you'll get AC with that.
you are right. I make a mistake there. So I got WA after solve CE problem.
Now I fix it and got AC. Thanks.