• 模板:统计1~n内x的个数


    http://noi.openjudge.cn/ch0105/40/

    40:数1的个数-拓展变形

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    给定一个十进制正整数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”。

    输入
    正整数n。1 <= n <= 10000。
    输出
    一个正整数,即“1”的个数。
    样例输入
    12
    样例输出
    5


    #include <iostream>
    #include <cstdio>
    #include <cstring>                  //for memset
    #include <algorithm>                //for max
    using namespace std;
    
    int cntone(int n,int x)
    {
        int cnt=0,i=0,tmp;
        for(i=0;i<=n;i++)//1~n
        {
            tmp=i;
            while(tmp)
            {
                if(tmp%10==x)
                    cnt++;
                tmp/=10;
            }
        }
        return cnt;
    }
    int main()
    {
        int n,x;
        while(cin>>n>>x)
        {
            cout<<cntone(n,x)<<endl;
        }
    }
    time:nlog(n) 计算数字 x 在 1-n 中出现的次数

    找规律:

    #include <iostream>
    #include <cstdio>
    #include <cstring>                  
    #include <algorithm>               
    using namespace std;
    /*只求1的个数*/
    int cntone(int n)
    {
        int cnt=0;
        for(int i=1;i<=n;i*=10)//1~n
        {
            int a=n/i,b=n%i;
            cnt+=(a+8)/10*i+((a%10)==1?b+1:0);  //之所以补8,是因为当百位为0,则a/10==(a+8)/10,当百位>=2,补8会产生进位位,效果等同于(a/10+1)
        }
        return cnt;
    }
    int main()
    {
        int n;
        while(cin>>n)
        {
            cout<<cntone(n)<<endl;
        }
    }
    time:logn 计算数字 1 在 1-n 中出现的次数
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    // 计算数字 X (不包括0)在 1-n 中出现的次数。
    // X = 0 时,规律与上面给出的规律不同,需要另行考虑。
    int cntone(int n,int x)
    {
        int cnt=0,tmp;
        for(int i=1; tmp=n/i; i*=10)//1~n
        {
            cnt += (tmp/10)*i;
            int cur = tmp%10;
            if(cur>x)
            {
                cnt+=i;
            }
            else if(cur == x)
            {
                cnt+=n-tmp*i+1;
            }
        }
        return cnt;
    }
    // X = 0 时
    int cntzero(int n)
    {
        int cnt=0,k;
        for(int i=1; (k=n/i)/10; i*=10)
        {
            cnt+= (k/10)*i;
            if(k%10==0)
            {
                cnt+=n-k*i+1-i;
            }
        }
        return cnt;
    }
    //合并:计算数字 X 在 1-n 中出现的次数,对 X 从 0 到 9 都有效:
    int cnts(int n,int x)
    {
        int cnt=0,k;
        for(int i=1; k=n/i; i*=10)
        {
            int high=k/10;
            if(x==0)
            {
                if(high) high--;
                else break;
            }
            cnt+=high*i;
            int cur=k%10;
            if(cur>x) cnt+=i;
            else if(cur==x) cnt+=n-k*i+1;
        }
        return cnt;
    }
    int main()
    {
        int n,x;
        while(cin>>n>>x)
        {
            cout<<cntone(n,x)<<" "<<cntzero(n)<<" "<<cnts(n,x)<<endl;
        }
    }
    计算数字 X 在 1-n 中出现的次数,对 X 从 0 到 9 都有效
    2. 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int cnts(int x)
    {
        int cnt=0;
        while(x)
        {
            if(x%2==1) cnt++; //n&1
            x/=2; //n>>=1;
        }
        return cnt;
    }
    
    int main()
    {
        int n;
        while(~scanf("%d",&n),n)
        {
            int cnt=cnts(n);
            while(n++)
            {
                if(cnt==cnts(n))
                {
                    cout<<n<<endl;
                    break;
                }
            }
        }
    }
    poj 2453
     51nod 1042 

     数字0-9的数量

  • 相关阅读:
    C语言的特点与缺点
    C语言的特点与缺点
    HDU1234 开门人和关门人
    HDU1234 开门人和关门人
    B00014 C++实现的AC自动机
    B00014 C++实现的AC自动机
    HDU4716 A Computer Graphics Problem
    HDU4716 A Computer Graphics Problem
    I00029 C语言程序-打印九九乘法表
    I00029 C语言程序-打印九九乘法表
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7904225.html
Copyright © 2020-2023  润新知