题解

210 条题解

  • -1
    @ 2018-07-25 09:01:26

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    int n,x,sum=0,m;
    cin>>n >>x;
    for(int i=1;i<=n;i++){
    int y=i;
    do{
    m=y%10;
    if(m==x)
    sum++;
    y=y/10;
    }while(y>=1);
    }
    cout<<sum;
    return 0;
    }

  • -1
    @ 2018-07-25 01:51:13

    不用对\(1,\cdots,n\)的暴力数位遍历。按\(10^m\)将\(1,\cdots,n\)划分为若干个组,每组对应个位数、十位数等数位中出现的个数,通过数学计算直接加总。复杂度从\(O(n\log(n))\)降到\(O(\log(n))\)。

    被数学函数坑了,忘记了有\(log10\),用换底公式结果出现了
    cpp
    floor(log(1000)/log(10))=2
    的情况……哪位路过大神还请能解释下这是为什么,万谢!

    以下是代码:

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        int n, x, did, m;
        cin >> n >> x;
        did = 0; // did is appearance times of x
        m = (int)floor(log10(n)); // m is the digit capacity of n
                                  // Note: floor(log(n) / log(10)) will go wrong when n=1e3 because floor causes loss in division!!
                                  //cout << log(n) / log(10) << " " << floor(log(n) / log(10)); // results: 3 2
        if (x) // 0 is a little bit tricky
        {
            for (int i = 1; i <= m; i++) // for complete 10^i cycle
                did += n / (int)pow(10, i) * (int)pow(10, i - 1);
            did += (n % 10 >= x) ? 1 : 0;   // first digit
            for (int i = 1; i <= m; i++)    // other digits
                if ((n / (int)pow(10, i)) % 10 > x)
                    did += (int)pow(10, i);
                else if ((n / (int)pow(10, i)) % 10 == x)
                    did += n % (int)pow(10, i) + 1;
        }
        else
        {
            for (int i = 1; i <= m; i++) // for complete 10^i cycle
                did += (n / (int)pow(10, i) - 1) * (int)pow(10, i - 1) + 1;
            // no first digit's contribution
            for (int i = 1; i < m; i++) // other digits
                if ((n / (int)pow(10, i)) % 10 > 0) // not 0 at i+1-th position
                    did += (int)pow(10, i) - 1;
                else if ((n / (int)pow(10, i)) % 10 == 0) // there is a 0 at i+1-th position
                    did += n % (int)pow(10, i); // not +1 as the non-zero number case
        }
    
        cout << did;
    
        return 0;
    }
    
  • -1
    @ 2018-07-21 15:43:08

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main(){
    int n,m,t,ts;

    cin>>n>>m;
    for(int i=1;i<=n;i++){
    ts=i;
    while(ts){
    if(ts%10==m)
    t++;
    ts=ts/10;

    }
    }
    cout<<t;
    return 0;
    }

  • -1
    @ 2018-06-27 16:50:40

    数据很水,只要一个模拟就可以通过了

    AC code:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        int a,m,t=0,d;
        cin>>a>>m;
        for(int i=1;i<=a;i++)
        {
            d=i;
            while(d>=10){
                if(d%10==m) t++;
                d=d/10; 
            }
            if(d==m) t++;
        }
        cout<<t;
        return 0;
     } 
    
  • -1
    @ 2018-06-15 19:51:12

    简单
    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    int n,x,sz[1000001];
    long long sum=0;
    cin>>n>>x;
    for(int i=1;i<=n;i++)
    {
    sz[i]=i;
    }
    for(int i=1;i<=n;i++)
    {
    while(sz[i]!=0)
    {
    int g=sz[i]%10;
    sz[i]=sz[i]/10;
    if (g==x) sum++;
    }
    }
    cout<<sum<<endl;
    return 0;
    }

  • -1
    @ 2018-06-01 22:58:32

    #include <iostream>
    using namespace std;
    /*
    试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?
    例如,在 1 到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。
    */
    int main()
    {
    int i, n, x, k = 0, j;
    scanf_s("%d %d", &n, &x);
    for (i = 1; i <= n; i++) {
    j = i;
    while (j % 10 == x) {
    k++;
    j = j / 10;
    }
    }
    printf("%d", k);
    system("pause");
    return 0;
    }

  • -1
    @ 2018-05-27 16:52:40

    #1 Accepted 3ms 264.0 KiB
    #2 Accepted 2ms 256.0 KiB
    #3 Accepted 556ms 380.0 KiB
    #4 Accepted 23ms 256.0 KiB
    #5 Accepted 690ms 384.0 KiB
    #6 Accepted 412ms 348.0 KiB
    #7 Accepted 3ms 340.0 KiB
    #8 Accepted 367ms 380.0 KiB
    #9 Accepted 679ms 256.0 KiB
    #10 Accepted 664ms 364.0 KiB

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <sstream>
    using namespace std;
    int main()
    {
    string s;
    int n,sum=0,x;
    cin>>n>>x;
    for(int i = 1;i <= n;i++)
    {
    stringstream ss;
    ss<<i;
    ss>>s;
    for(int j = 0;j <= s.length();j++)
    if(s[j] == x+0x30) sum++;
    }
    cout<<sum;
    return 0;
    }

  • -1
    @ 2018-05-14 20:10:12
    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2018\5\14 0014
     * Time: 18:09
     */
    
    //试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1 到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。
    
    class count{
        protected $min = 1;
        protected $max;
        protected $num;
    
        public function outPutCount(){
            $count = 0;
            for ($i = $this->min; $i <= $this->max; $i ++){
                $count = $count + substr_count($i,$this->num);
            }
            return $count;
        }
    
        public function setMax($max){
            $this->max = $max;
            return $this;
        }
    
        public function setNum($num){
            $this->num = $num;
            return $this;
        }
    
    }
    
    $stdin = fopen('php://stdin','r');
    $data = trim(fgets($stdin));
    $lists = explode(' ',$data);
    $max = $lists[0];
    $num = $lists[1];
    $counter = new count();
    $count = $counter->setMax($max)->setNum($num)->outPutCount();
    echo $count;
    
  • -2
    @ 2018-05-25 14:53:49
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int main()
    {
        int ans=0,n,x;
        scanf("%d%d",&n,&x);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=10000000;j=j*10)
                if(i>=j)if(i/j%10==x)ans++;
        printf("%d",ans);
        return 0;
    }
    
  • -4
    @ 2018-06-04 17:27:12

    参考刘汝佳《算法竞赛入门经典》。
    用sprintf
    #include<stdio.h>
    #include<string.h>
    char s[15];
    int main()
    {
    int n,x;
    scanf("%d%d",&n,&x);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    sprintf(s,"%d",i);
    for(int j=0;j<strlen(s);j++)
    if(s[j]-'0'==x)ans++;
    }
    printf("%d\n",ans);
    return 0;
    }

信息

ID
1848
难度
5
分类
(无)
标签
递交数
16396
已通过
5730
通过率
35%
被复制
33
上传者