/* @问题:不要62 @time:20160419 @Author:Alex @数位DP */ #include<stdio.h> #include<string.h> //数位DP算法 int dp[10][10]; void Digital() { int i=0,j=0,k=0; memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(i=1;i<=7;i++) { for(j=0;j<=9;j++) { for(k=0;k<=9;k++) { if((j != 4) && !(j== 6 &&k==2)) { dp[i][j] += dp[i-1][k]; } } } } } // int find(int n) { int digit[10]; int length = 0; int answer = 0; int count_i =0; int count_j =0; Digital(); while(n>0)//将数值转换为字符串 { digit[++length] = n%10; n = n/10; } digit[length+1]=0; for(count_i=length;count_i;count_i--) { for(count_j=0;count_j<digit[count_i];count_j++) { if(count_j != 4 && !(digit[count_i+1]==6 && count_j==2)) { answer += dp[count_i][count_j]; } } if(digit[count_i]==4 || (digit[count_i]==2 && digit[count_i+1]==6)) { break; } } return answer; } int main() { int m,n; while(scanf("%d%d",&n,&m)!=EOF && (n!=0 && m!=0) ) { //因为find函数中并没有考虑n是不是不幸数的情况,所以m+1只算了1~m,而n只算了1~n-1,这两者相减才是正确答案 printf("%d\n",find(1+m)-find(n)); } return 0; }