3 条题解

  • 2
    @ 2024-2-18 12:08:54

    这一道题非常简单,甚至没用数组我就解决了!

    已AC,请放心食用

    #include <iostream>
    using namespace std;
    int main()
    {
        int n, sum, y;
        cin >> n;
        for (int i = 0; i < 10; i++)
        {
            sum = 0;
            for (int j = 1; j <= n; j++)
            {
                y = j;
                while (y > 0)
                {
                    if (y % 10 == i)
                    {
                        sum++;
                    }
                    y /= 10;
                }
            }
            cout << sum << endl;
        }
        return 0;
    }
    
    • 1
      @ 2023-1-20 18:41:57

      直接上代码,没什么说的,要说就说太简单了。

      #include <iostream>
      using namespace std;
      int n,a[10001],c;
      int main()
      {
          cin >> n;
          for(int i=1;i<=n;i++)
          {
              c=i;
              while(c>0)
              {
                  a[c%10]++;
                  c/=10;
              }
          }
          for(int i=0;i<=9;i++)
          {
              cout << a[i] << endl;
          }
      }
      
      • 1
        @ 2022-11-21 11:07:25

        我的想法是创建一个数组(用下标表示每个数字)以存储每个数字出现的次数。之后在遍历每个数字的每一位,将数组中相应元素加一。

        //numCount是存储每个数出现次数的数组
        for (int i = 1 ; i <= n ; i++)
        {
            int x = i;
            while (x > 0)
            {
                numCount[x % 10]++;
                x /= 10;
            }
        }
        
        • 1

        信息

        ID
        178
        时间
        1000ms
        内存
        16MiB
        难度
        1
        标签
        递交数
        78
        已通过
        56
        上传者