Sorry that I took time to post a reply. My phone line went dead and I'm off the 'Net.
Yes, it is possible to do faster I/O operations with C++ too. But the question is, would we like to do that? First let me show an example of doing fast (block) input handling for reading non-negative integers.
[cpp]
#include <cstdio>
#include <iostream>
using namespace std;
class myclass {
private:
int number;
static const int MAXSIZE = 1000000;
static char inpbuffer[MAXSIZE], *ptr;
static bool read;
public:
int getNumber() {
return number;
}
void setNumber(int number) {
myclass::number = number;
}
friend ostream& operator<<(ostream&, myclass&);
friend bool operator>>(istream&, myclass&);
};
bool myclass::read;
char myclass::inpbuffer[myclass::MAXSIZE];
char* myclass::ptr;
ostream& operator<<(ostream& out, myclass& obj) {
out<<obj.getNumber();
return out;
}
bool operator>>(istream& in, myclass& obj) {
if (myclass::read==false) {
in.read(myclass::inpbuffer,sizeof(myclass::inpbuffer));
// fread(myclass::inpbuffer,sizeof(myclass::inpbuffer),sizeof(char),stdin);
myclass::ptr = myclass::inpbuffer;
myclass::read = true;
}
int num = 0;
while (true) {
while (*myclass::ptr && (*myclass::ptr<'0' || *myclass::ptr>'9'))
myclass::ptr++;
if (*myclass::ptr=='\0') return false;
while (*myclass::ptr && *myclass::ptr>='0' && *myclass::ptr<='9') {
num *= 10;
num += *myclass::ptr-'0';
myclass::ptr++;
}
break;
}
if (*myclass::ptr) {
obj.setNumber(num);
return true;
} else return false;
}
int main() {
myclass myobj;
while (cin>>myobj) {
cout<<myobj<<endl;
// printf("%d\n",myobj.getNumber());
}
return 0;
}
[/cpp]
The same thing can be done in C, using:
[c]
#include <stdio.h>
#define MAXSIZE 1000000
char inpbuffer[MAXSIZE], *ptr;
int read;
int getNumber() {
if (!read) {
fread(inpbuffer,sizeof(inpbuffer),sizeof(char),stdin);
ptr = inpbuffer;
read = 1;
}
int num = 0;
while (1) {
while (*ptr && (*ptr<'0' || *ptr>'9'))
ptr++;
if (*ptr=='\0') return -1;
while (*ptr && *ptr>='0' && *ptr<='9') {
num *= 10;
num += *ptr-'0';
ptr++;
}
break;
}
if (*ptr) {
return num;
} else return -1;
return num;
}
int main() {
int num;
while ((num=getNumber())!=-1)
printf("%d\n",num);
return 0;
}
[/c]
The C++ code runs a bit slower compared to the C code. But it remains higly comparable. I verified the codes in Visual C++ .Net and GCC 3.2.2 from DJGPP on Windows XP and GCC 3.2 on RedHat linux 8.0.
If you'd notice, you'd see that in the C++ code I've tried to go with the OOP paradigm. Thanks to the nifty feature of operator overloading of C++, looking at the main method you'd see that life had been made a lot easier. But, if you'd have a look at the class definition, you'd clearly see how many stupid things I had to do for this simple task. The task was simply to read non-negative integers (reading by block of course), why on earth should I instantiate objects with an integer data member only?
OOP is good. In my undergraduate program my introduction to programming was with JAVA, for that reason I tried my level best to be an OO purist. But as I moved on to programming contests, I realized that given the limited time to solve a problem, it's better to give up those ideal thoughts. OO features are quite useful for solving problems, even in real time programming contests, however it's not really wise to follow OOP strictly everywhere in a contest. For application programming given the freedom, I'd definitely go for OOP. But for real-time contests (not for online contests

) I'd suggest to think if it's necessary at all.
I prefer not to say anything about doing faster I/O with JAVA. UVA OJ doesn't provide reasonable support for JAVA. The way it is now, doing simple I/O seems quite painful. I hope UVA would provide the required support as they promised to do it "soon".