吉比特笔试题解

#include <bits/stdc++.h>
using namespace std;

const int N = 10;

int dp[N][5][2];
int l, num[N];
int dat[4] = {2, 0, 1, 8};

int dfs(int pos, int pn, int lim)
{
  if(pos == -1) {
    if(pn == 4) return 1;
    return 0;
  }
  if(dp[pos][pn][lim] != -1 && (!lim))
    return dp[pos][pn][lim];
  int top = lim ? num[pos] : 9;
  int rtn = 0;
  for(int i = 0; i <= top; ++ i) {
    int npn = 4;
    if(pn < 4) {
      if(dat[pn] == i) npn = pn + 1;
      else npn = pn;
    }
    rtn += dfs(pos - 1, npn, lim&&(i==top));
  }
  if(!lim) dp[pos][pn][lim] = rtn;
  return rtn;
}

int main()
{
  int x;
  scanf("%d", &x);
  memset(dp, -1, sizeof(dp));
  l = 0;
  while(x) {
    num[l ++] = x % 10;
    x /= 10;
  }
  printf("%d\n", dfs(l - 1, 0, 1));
}

 

posted @ 2019-04-10 20:58  Roni_i  阅读(820)  评论(0编辑  收藏  举报