国产成人久久777777-国产农村妇女毛片精品久久-精品少妇人妻AV一区二区-少妇人妻精品一区二区三区-无码人妻精品一区二区

1049. Counting Ones (30)[計算1 的個數]——PAT (Advanced -電腦資料

電腦資料 時間:2019-01-01 我要投稿
【www.gydabaoji.com - 電腦資料】

   

題目信息

    1049. Counting Ones (30)

    時間限制10 ms

    內存限制65536 kB

    代碼長度限制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

解題思路

    《編程之美》中計算0到N包含數字1的個數問題

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>

最新文章
主站蜘蛛池模板: 广东省| 德江县| 浮山县| 高淳县| 海城市| 洛扎县| 瑞安市| 榕江县| 昌吉市| 桓仁| 维西| 静海县| 鹿泉市| 石楼县| 农安县| 寻乌县| 安达市| 五常市| 多伦县| 苗栗市| 仁寿县| 岳阳县| 河津市| 大新县| 漳浦县| 精河县| 郎溪县| 乌鲁木齐市| 本溪市| 兴仁县| 时尚| 台山市| 边坝县| 柏乡县| 盐城市| 杭州市| 扬州市| 邢台县| 津南区| 和田市| 合阳县|