24h購物| | PChome| 登入
2011-08-05 11:37:32| 人氣4,367| 回應0 | 上一篇 | 下一篇

[Ver 2013.3.16][C++ JAVA] 優化輸入+template

推薦 0 收藏 0 轉貼0 訂閱站台

× 有錯誤的話, 請通報我, 感謝您
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;
    }

台長: Morris
人氣(4,367) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 社會萬象(時事、政論、公益、八卦、社會、宗教、超自然) | 個人分類: 解題技巧 |
此分類下一篇:[分析] 生成排列大車拼
此分類上一篇:[分析] ZJ 排序大車拼

TOP
詳全文