zoukankan      html  css  js  c++  java
  • 统计"1"个数问题

    问题:

    给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数。
    例如:N=2时 1,2出现了1个 “1” 。

    N=12时 1,2,3,4,5,6,7,8,9,10,11,12。出现了5个“1”。

    回答:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    long long int Count(long long int n){
        long long int count = 0;
        while(n){
            count += (n % 10 == 1)?1:0;
            n = n / 10;
        }
        return count;
    }
    int main()
    {
        long long int n,i,count;
        while(scanf("%lld",&n) != EOF){
            count = 0;
            for(i = 1;i <= n;i++){
                count += Count(i);
            }
            printf("%lld ",count);
        }
        return 0;
    }

  • 相关阅读:
    Redis 分布式锁
    Angular VS Blzaor
    Chorme 跨域的快捷解决
    旋转3角形
    .Netcore AD 操作
    .Netcore 2.2 和3.1 的模板
    Command3
    CSS Selector
    弹性盒子
    Label_strange_labels
  • 原文地址:https://www.cnblogs.com/benchao/p/4509975.html
Copyright © 2011-2022 走看看