Simple game (easygame)

Simple game (easygame) ⁡ \operatorname{easygame (easygame)} Simple single trip play ( E A S Y G A m E )

Topic link: SSL competition 1507 ⁡ \operatorname{SSL competition\ 1507}S S L ratio race . 1 . 5 0 . 7 

topic

One day, little R was going to find little h to go swimming. When he found little h, he found that little h was writing a column of numbers in pain, 1 11 2 2 2 3 3 3 … … n n n , so I asked little h the reason for his pain, and little h told him that now he has to count1.. n 1..n. 1 . . N- these numbers inside,11How many times does 1 appear, such asn = 11 n=11n=1 At 1 , there are1, 10, 11 1,10,111,10,1 1 appeared4 44 times1 11 , nown, n,n , can you give the answer quickly?

enter

One line, an integer nnn

Output

An integer representing 1.. n 1..n. 1 . . N- in111 number of occurrences.

Sample input

11

Sample output

4

data range

For 30% 30\%. 3 0 % Data:n-<= 1000; n-<= 1000;n<=1 0 0 0 ;
for100% 100\%. 1 0 0 % Data:n-<= maxlongint; n-<= maxlongint;n<=maxlongint;

Ideas

This question is a mathematics question and should be discussed separately.

We are asking
for one by one: For the ones, 1 1 appears here for every ten1 Ge111 ; For ten digits,10 10appears here for every hundred1 0 Ge111 ; For hundreds of places, there will be100 100if there are no thousands1 0 0 Ge111

The number in front of it is the number of each, or ten, hundred, etc.
(Think 233 2332 3 3 , for the tens place, there is2 22 Ge101010

Then look at this bit, you can find that if this bit is 0 00 , it is normal. If this bit is greater than1 11 , there will be one more each, or ten, one hundred, etc.

But if this bit is equal to 1 11 ?
Then we found that the number behind it is how many more times+ 1 +1+ 1 .
Why add one? Because for example10 101 0 , when you see the tens place, although the number behind is0 00 , but it can be seen that1 1appears in the ten digits1 time1 11 . Because10 10. 1 0 which is a ten111 of.

Code

#include<cstdio>
#define ll long long

using namespace std;

ll n, ans;
ll more, times, sheng;

int main() {
    
    
	scanf("%lld", &n);//读入
	
	for (ll wei = 1; wei <= n; wei *= 10) {
    
    //枚举每一位
		more = n % (wei * 10) / wei;//这一位
		times = n / (wei * 10);//它前面组成的数字
		sheng = n % wei;//它后面组成的数字
		if (more > 1) ans += times * wei + wei;
			else if (more == 1) ans += times * wei + sheng + 1;//分类讨论
				else ans += times * wei;
	}
	
	printf("%lld", ans);//输出
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/108148776