33,008
社区成员
发帖
与我相关
我的任务
分享
public static int lucky(int start, int end)
{
int value = 0;
if (start <= end)
{
if (start <= 0) start = 1;
if (end >= 1000000) end = 999999;
int[] c2 = { 1, 0, 0, 0, 0 }, c4 = { 1, 0, 0, 0, 0 }, c = { 8, 0, 0, 0, 0 };
for (int j, sum = 100, i = 1; i < 5; sum *= 10, i++) c[i] = sum - (c2[i] = c2[j = i - 1] + c[j]) - (c4[i] = (c2[j] << 1) + 10 * c4[j] + c[j]);
value = lucky(end, c2, c) - (start == 1 ? 0 : lucky(start - 1, c2, c));
}
return value;
}
private static int lucky(int end, int[] count2, int[] count)
{
int value = 0;
List<int> mod = new List<int>();
while (end > 9)
{
mod.Add(end % 10);
end /= 10;
}
mod.Add(end);
int m, lm = 0;
bool not62 = true;
for (int c, ci, i = mod.Count - 1; not62 && i > 0; not62 = m != 4 && (m != 2 || lm != 6), lm = m, i--)
{
if ((m = mod[i]) != 0)
{
value += m * (c = count2[ci = i - 1] + count[ci]);
if (lm == 6 && m > 2) value -= c;
if (m > 4)
{
value -= c;
if (m > 6) value -= count2[ci];
}
}
}
if (not62)
{
value += (m = mod[0]);
if (m < 4)
{
if (lm != 6 || m < 2) value++;
}
else if (lm == 6) value--;
}
return value - 1;
}
public static int lucky(int start, int end)
{
int value = 0;
if (start <= end)
{
if (start <= 0) start = 1;
if (end >= 1000000) end = 999999;
int[] c2 = { 1, 0, 0, 0, 0 }, c4 = { 1, 0, 0, 0, 0 }, c = { 8, 0, 0, 0, 0 };
for (int j, sum = 100, i = 1; i < 5; sum *= 10, i++) c[i] = sum - (c2[i] = c2[j = i - 1] + c[j]) - (c4[i] = (c2[j] << 1) + 10 * c4[j] + c[j]);
value = lucky(end, c2, c) - (start == 1 ? 0 : lucky(start - 1, c2, c)) - 1;
}
return value;
}
private static int lucky(int end, int[] count2, int[] count)
{
int value = 0;
List<int> mod = new List<int>();
while (end > 9)
{
mod.Add(end % 10);
end /= 10;
}
mod.Add(end);
int m, lm = 0;
bool not62 = true;
for (int c, ci, i = mod.Count - 1; not62 && i > 0; not62 = m != 4 && (m != 2 || lm != 6), lm = m, i--)
{
if ((m = mod[i]) != 0)
{
value += m * (c = count2[ci = i - 1] + count[ci]);
if (lm == 6 && m > 2) value -= c;
if (m > 4)
{
value -= c;
if (m > 6) value -= count2[ci];
}
}
}
if (not62)
{
value += (m = mod[0]);
if (m < 4)
{
if (lm != 6 || m < 2) value++;
}
else if (lm == 6) value--;
}
return value;
}
#include <iostream>
using namespace std;
int a[] = {1,9,80,711,6319,56160};
int f(int m, int n){
return (fx(n)-f(m-1));
}
int fx(int n){
byte d[7];
for (int i=0;i<=5;i++){
d[i] = (n % 10);
n /= 10;
}
return fy(d);
}
int fy(byte d[]){
...
}
int main()
{
int n, m, result;
while(cin >> n >> m)
{
if(0==n && 0== m)
break;
result = f(m,n);
cout < < result < < endl;
}
return 0;
}
//★ d[4] = 3
+ a(4)*3 //[00000, 29999]
//★ d[3] = 6
+ a(3)*5 //[30000,33999]∪[35000,35999]
//★ d[2] = 5
+ a(2)*4 //[36000,36399]
- a(2) //排除 [36200,36299]
//★ d[1] = 7
+ a(1)*6 //[36500,36539]∪[36550,36569]
- a(0) //排除 [36562,36562]
//★ d[0] = 4
+ a(0)*4 //[36570,36573]
---------
= 22809
int fy(d[]){
d[k]++; //个位不向下拆分,要算上自己
count=0;
for(k=5;k>0;k--){
//比如 d[3]==7,统计 [##0000, ##6999 ] 区间
if (d[k]<=4){
count += a[k] * d[k];
}else{
count += a[k] * (d[k]-1); //排除##4###
}
//排除 #62###
if ((d[k+1]=6) && (d[k]>2))
count -= a[k];
//排除 ##62##
if ((d[k]>6) && (k>0))
count -= a[k-1];
}
return count;
}
#include <string.h>
#include <stdio.h>
int b[7] = {1, 9, 80, 711, 6319, 56160, 499121};
int g(char *a);
int f(char *a)
{
int len;
len = strlen(a);
if (len == 0)
return 0;
else if (len == 1)
{
if (a[0] >= '4')
return a[0] - '0';
else
return a[0] - '0' + 1;
}
else
{
switch(a[0]-'0')
{
case 0:
case 1:
case 2:
case 3: return f(a+1) + (a[0] - '0')*b[len-1];
case 4: return 4*b[len-1];
case 5: return 4*b[len-1] + f(a+1);
case 6: return 5*b[len-1] + f(a+1) - g(a+1);
default: return (a[0] - '0' - 1)*b[len-1] + f(a+1) - b[len - 2];
}
}
}
int g(char *a)
{
if (a[0] == '\0')
return 0;
switch(a[0] - '0')
{
case 0:
case 1: return 0;
case 2: return (a[1] == '\0')?1:f(a+1);
default: return b[strlen(a) - 1];
}
}
int main()
{
int m, n;
int k, t;
char a[10];
while (scanf("%d %d", &m, &n) != EOF)
{
if (m == 0 && n == 0)
break;
if (0 == m)
a[0] = '\0';
else
sprintf(a, "%d", m-1);
k = f(a);
sprintf(a, "%d", n);
t = f(a);
printf("%d\n", t - k);
}
}