2011-08-05 11:37:32Morris
[Ver 2013.3.16][C++ JAVA] 優化輸入+template
× 有錯誤的話, 請通報我, 感謝您
C 沒有 inline, C++ 才有, 使用時, 請注意
使用作法 :
int x;
while(ReadInt(&x) != EOF)
printf("%d\n", x);
double s;
while(ReadDouble(&s) != EOF)
printf("%lf\n", s);
inline int ReadInt(int *x) {
static char c, neg;
while((c = getchar()) < '-') {if(c == EOF) return EOF;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
inline double ReadPoint() {
static char c;
int p = 0;
double t = 1;
while((c = getchar()) >= '0')
t /= 10, p = (p << 3) + (p << 1) + (c-'0');
return (double)p*t;
}
inline int ReadDouble(double *x) {
static char c, neg;
while((c = getchar()) < '-') {if(c == EOF) return EOF;}
if(c == '.') {*x = ReadPoint();return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
*x = (*x)*10 + c-'0';
if(c == '.') *x += ReadPoint();
*x *= neg;
return 0;
}
inline int readchar() {
const int N = 1048576;
static char buf[N];
static char *p = buf, *end = buf;
if(p == end) {
if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
p = buf;
}
return *p++;
}
inline int ReadInt(int *x) {
static char c, neg;
while((c = readchar()) < '-') {if(c == EOF) return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = readchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
C++ 通用模板
#include <iostream>
#include <cstdio>
using namespace std;
template<typename TYPE>
inline int ReadNum(TYPE& x) {
static char c, neg;
static long long y;
while((c = getchar()) < '-') {if(c == EOF)return EOF;}
neg = (c == '-') ? -1 : 1;
if(c != '.') {
y = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
y = (y<<3) + (y<<1) + c-'0';
x = y;
}
if(c == '.') {
static double t;
t = 1;
while((c = getchar()) >= '0')
t = t/10, x += (c-'0')*t;
}
x *= neg;
return 1;
}
int main() {
double Double;
int Int;
ReadNum(Double);
ReadNum(Int);
cout << Double << endl;
cout << Int << endl;
return 0;
}
Java
public static int parseInt() {
int x = 0, c, neg;
try {
while ((c = System.in.read()) < '-') {
if (c == -1)
System.exit(0);
}
neg = (c == '-') ? -1 : 1;
x = neg == 1 ? c - '0' : 0;
while ((c = System.in.read()) >= '0') {
x = x * 10 + c - '0';
}
} catch (Exception e) {
}
return x;
}
C 沒有 inline, C++ 才有, 使用時, 請注意
使用作法 :
int x;
while(ReadInt(&x) != EOF)
printf("%d\n", x);
double s;
while(ReadDouble(&s) != EOF)
printf("%lf\n", s);
inline int ReadInt(int *x) {
static char c, neg;
while((c = getchar()) < '-') {if(c == EOF) return EOF;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
inline double ReadPoint() {
static char c;
int p = 0;
double t = 1;
while((c = getchar()) >= '0')
t /= 10, p = (p << 3) + (p << 1) + (c-'0');
return (double)p*t;
}
inline int ReadDouble(double *x) {
static char c, neg;
while((c = getchar()) < '-') {if(c == EOF) return EOF;}
if(c == '.') {*x = ReadPoint();return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
*x = (*x)*10 + c-'0';
if(c == '.') *x += ReadPoint();
*x *= neg;
return 0;
}
inline int readchar() {
const int N = 1048576;
static char buf[N];
static char *p = buf, *end = buf;
if(p == end) {
if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
p = buf;
}
return *p++;
}
inline int ReadInt(int *x) {
static char c, neg;
while((c = readchar()) < '-') {if(c == EOF) return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = readchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
C++ 通用模板
#include <iostream>
#include <cstdio>
using namespace std;
template<typename TYPE>
inline int ReadNum(TYPE& x) {
static char c, neg;
static long long y;
while((c = getchar()) < '-') {if(c == EOF)return EOF;}
neg = (c == '-') ? -1 : 1;
if(c != '.') {
y = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
y = (y<<3) + (y<<1) + c-'0';
x = y;
}
if(c == '.') {
static double t;
t = 1;
while((c = getchar()) >= '0')
t = t/10, x += (c-'0')*t;
}
x *= neg;
return 1;
}
int main() {
double Double;
int Int;
ReadNum(Double);
ReadNum(Int);
cout << Double << endl;
cout << Int << endl;
return 0;
}
Java
public static int parseInt() {
int x = 0, c, neg;
try {
while ((c = System.in.read()) < '-') {
if (c == -1)
System.exit(0);
}
neg = (c == '-') ? -1 : 1;
x = neg == 1 ? c - '0' : 0;
while ((c = System.in.read()) >= '0') {
x = x * 10 + c - '0';
}
} catch (Exception e) {
}
return x;
}
上一篇:[分析] ZJ 排序大車拼
下一篇:[分析] 生成排列大車拼