I don't know why this got CE.

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
User avatar
shines
New poster
Posts: 4
Joined: Fri Apr 07, 2006 1:58 pm

I don't know why this got CE.

Post by shines » Fri Apr 07, 2006 2:03 pm

This go well on my machine, but I got CE. Why?

Code: Select all

deleted..
Last edited by shines on Sat Apr 08, 2006 6:17 am, edited 1 time in total.

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho » Fri Apr 07, 2006 5:57 pm

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.

User avatar
shines
New poster
Posts: 4
Joined: Fri Apr 07, 2006 1:58 pm

Post by shines » Fri Apr 07, 2006 6:35 pm

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?

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Fri Apr 07, 2006 9:18 pm

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) ...)

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

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

Post by Krzysztof Duleba » Fri Apr 07, 2006 9:35 pm

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.

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Sat Apr 08, 2006 12:26 am

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.

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

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

Post by Cho » Sat Apr 08, 2006 5:33 am

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)).

User avatar
shines
New poster
Posts: 4
Joined: Fri Apr 07, 2006 1:58 pm

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

Post by shines » Sat Apr 08, 2006 6:16 am

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.

Post Reply

Return to “C++”