日韩欧美一 , 欧美午夜激情视频 , 国产精品一二三 , 日韩av电影一区二区三区,av官网在线,少妇高潮在线,国产精品亚洲综合色区韩国

1049. Counting Ones (30)[計(jì)算1 的個(gè)數(shù)]——PAT (Advanced -電腦資料

電腦資料 時(shí)間:2019-01-01 我要投稿
【www.edhbe.com - 電腦資料】

   

題目信息

    1049. Counting Ones (30)

    時(shí)間限制10 ms

    內(nèi)存限制65536 kB

    代碼長(zhǎng)度限制16000 B

    The task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form. of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12.

    Input Specification:

    Each input file contains one test case which gives the positive N (<=2^30).

    Output Specification:

    For each test case, print the number of 1’s in one line.

    Sample Input:

    12

    Sample Output:

    5

解題思路

    《編程之美》中計(jì)算0到N包含數(shù)字1的個(gè)數(shù)問(wèn)題

AC代碼

<code class="language-c++ hljs cpp">#include<cstdio>int get(long long n){    long long cur = 0, before = 0, after = 0, i = 1, cnt = 0;    while ((n / i) != 0){        cur = n / i % 10;        before = n / i / 10;        after = n - (n / i) * i;        if (cur == 0){            cnt += before * i;        }else if (cur == 1){            cnt += before * i + after + 1;        }else {            cnt += (before + 1) * i;        }        i *= 10;    }    return cnt;}int main(){    long long a;    scanf("%lld", &a);    printf("%d\n", get(a));    return 0;}</cstdio></code>

最新文章