AcWing 338. 338. 计数问题
原题链接
简单
作者:
Easene
,
2025-03-29 14:11:34
· 黑龙江
,
所有人可见
,
阅读 1
import java.util.*;
import java.io.*;
public class Main
{
static int get(List<Integer> arr,int r,int l)
{
int res = 0;
for(int i = r; i >= l;i--)
{
res += arr.get(i) + res * 10;
}
return res;
}
static int power10(int x)
{
int res = 1;
while(x != 0)
{
res *= 10;
x--;
}
return res;
}
static int count(int n,int x)
{
if(n == 0) return 0;
List<Integer> l = new ArrayList<>();
while(n != 0)
{
l.add(n % 10);
n /= 10;
}
int len = l.size(),res = 0;
for(int i = len - 1 - (x==0?1:0);i >= 0; i--)
{
if(i < len - 1)
{
res += get(l, len - 1,i + 1) * power10(i);
if(x == 0) res -= power10(i);
}
if(l.get(i) > x) res += power10(i);
else if(l.get(i) == x) res += get(l,i - 1, 0) + 1;
}
return res;
}
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter log = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
String[] T = in.readLine().split("\\s+");
int a = Integer.parseInt(T[0]), b = Integer.parseInt(T[1]);
while(a != 0)
{
if(a > b)
{
int t = b;
b = a;
a = t;
}
for(int i = 0;i <= 9;i++)
{
log.print((count(b,i) - count(a,i)) + " ");
}
log.println();
T = in.readLine().split("\\s+");
a = Integer.parseInt(T[0]); b = Integer.parseInt(T[1]);
}
log.flush();
}
}