【UVa】[1225]Digit Counting

Problem Description

Loading...

Problem Analysis

/*Store integers in character form

then just check it*/

At first I thought it was like the case above: given a string of numbers and asked to find something

It turned out to be finding the occurrence counts of all digits in 1~N…

So I got tricked -.-

So I had to consider whether it would time out…

Then I still felt it was better to just precompute a table

After all, if this problem times out, it should be because many already-computed operations are repeated

/*(Also, the given sample output has a space after the last digit~ xi xi xi)*/

What a tricky problem. The sentence in the parentheses above is wrong

Although the problem’s sample output has spaces, the result is still required not to have spaces-.-

If there is a space at the end, it will get PE… -.-

AC code:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int a[10010][10];
int main() {
	int n,t;
//	int a[10010][10]; 放到这里在数据大的时候会WA
	for(int i=1; i<10010; i++) {
		for(int j=i; j; j/=10) {
			for(int k=i; k<10010; k++)
				a[k][j%10]++;
		}
	}
	scanf("%d",&n);
	while(n--) {
		scanf("%d",&t);
		for(int i=0; i<9; i++)
			printf("%d ",a[t][i]);
		printf("%d\n",a[t][9]);
	}
	return 0;
}

Note that the array needs to be placed outside int main…

-.- It should be the so-called overflow issue.

Also, here is a table-precomputation method written based on someone else’s code

The third loop used when building this table is different

(that is, the method of recording the previous data)

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int a[10010][10];
int main() {
	int n,t;
	for(int i=1; i<10010; i++) {
		for(int j=i; j; j/=10) {
			a[i][j%10]++;
		}
		for(int j=0; j<10; j++)
			a[i][j]+=a[i-1][j];
	}
	scanf("%d",&n);
	while(n--) {
		scanf("%d",&t);
		for(int i=0; i<9; i++)
			printf("%d ",a[t][i]);
		printf("%d\n",a[t][9]);
	}
	return 0;
}

PS:

While writing the AC code, I also accidentally wrote code for the first interpretation-.-

Okay~ I’ll keep it as a memento too

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main() {
	int n,t;
	int a[10010][10];
	for(int i=1; i<10010; i++) {
		for(int j=i; j; j/=10) {
			a[i][j%10]++;
		}
	}
	scanf("%d",&n);
	while(n--) {
		scanf("%d",&t);
		for(int i=0; i<10; i++)
			printf("%d ",a[t][i]);
		printf("\n");
	}
	return 0;
}

Problem link:【UVa】[1225]Digit Counting

Loading...