1. 程式人生 > >PAT甲級1049 Counting Ones【規律】

PAT甲級1049 Counting Ones【規律】

ostream mes typedef tro tps get stream clu n)

題目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456

題意:

給定n,問0~n中,1的總個數是多少。

思路:

問的是總個數,所以不需要考慮重復,只用考慮每一位上的貢獻就行了。

將數字分成三部分,left(共i位),now和right(共j位)

如果當前now是0, 那麽所有前i位是[0,left)的數字都+1個貢獻,這些數一共有$left*10^j$個

如果當前now是[2,9],那麽所有前i位是[0,left]的數字都+1個貢獻,這些數一共有$(left+1)*10^j$個

如果當前now是1,那麽這一位是1的數可以是,前i位為[0,left)的所有數,或是前i位剛好是left而後j位是[0,right]的。

一共有$left*10^j+right+1$個

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<map>
 4 #include<set>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<vector>
 9 #include<cmath> 
10 #include<stack>
11 #include<queue>
12
13 #define inf 0x7fffffff 14 using namespace std; 15 typedef long long LL; 16 typedef pair<string, string> pr; 17 18 LL n; 19 LL r; 20 21 int main() 22 { 23 LL p = 1; 24 cin>>n; 25 LL ans = 0; 26 LL tmp = n; 27 while(n){ 28 if(n % 10 == 0){ 29 //
cout<<n / 10 * p<<endl; 30 ans += n / 10 * p; 31 } 32 else if(n % 10 == 1){ 33 //cout<<(n / 10 * p) + r + 1<<endl; 34 ans += (n / 10 * p) + r + 1; 35 } 36 else{ 37 //cout<<(n / 10 + 1) * p<<endl; 38 ans += (n / 10 + 1) * p; 39 } 40 41 42 n /= 10; 43 p *= 10; 44 r = tmp % p; 45 } 46 47 cout<<ans<<endl; 48 return 0; 49 }

PAT甲級1049 Counting Ones【規律】