bjfu1281

思路挺简单的,但因为需要处理大数,所以就比较耗代码了。

/*
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;

//比较两个数s1和s2的大小,当s1<s2时返回真
bool lessthan(const char* s1, const char* s2, int len) {
    for (int i = 0; i < len; i++) {
        if (s1[i] < s2[i]) {
            return true;
        } else if (s1[i] > s2[i]) {
            return false;
        }
    }
    return false;
}

//检测是否栅栏数
bool judge(const char* ss, int len) {
    if (len < 3 || len % 2 == 0) {
        return false;
    }
    int d = len / 2;
    for (int i = 0; i < d; i++) {
        if (ss[i] != '1' || ss[len - i - 1] != '1') {
            return false;
        }
    }
    return true;
}

int lowercount(const char* ss, int len) {
    if (len < 3) {
        return 0;
    }
    if (len % 2 == 0) {
        return (len / 2 - 1) * 10;
    }
    if (judge(ss, len)) {
        return (len / 2 - 1) * 10 + ss[len / 2] - '0';
    }
    char str[2000];
    memset(str, '1', len);
    str[len] = 0;
    str[len / 2] = '0';
    if (lessthan(ss, str, len)) {
        return (len / 2 - 1) * 10;
    } else {
        return (len / 2 ) * 10;
    }
}


int main() {
//    freopen("data.in", "r", stdin);
    char a[2000], b[2000];
    int T, lena, lenb;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++) {
        scanf(" %s %s", a, b);
        lena = strlen(a);
        lenb = strlen(b);
        int ans = lowercount(b, lenb) - lowercount(a, lena);
        if (judge(b, lenb)) {
            ans++;
        }
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/moonbay/p/4279601.html