3 条题解

  • 1
    @ 2024-10-29 14:16:28
    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int n, x, count = 0;
        cin >> n >> x;
    
        for (int i = 1; i <= n; i++) {
            int temp = i;
            while (temp > 0) {
                if (temp % 10 == x) {
                    count++;
                }
                temp /= 10;
            }
        }
    
        cout << count << endl;
        return 0;
    }
    
    • 0
      @ 2025-3-31 14:34:58

      import java.util.Scanner; import java.util.ArrayList; public class Main2 {

      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          ArrayList<Integer> list = new ArrayList<>();
      
          //表示1到n的n
          int n = sc.nextInt();
      
          //表示要计数的数字
          int x = sc.nextInt();
      
          //集合中添加1到n的n
          for (int i = 1; i <= n; i++) {
              list.add(i);
          }
      
          //遍历集合,计算其中出现多少次x
          int count = 0;
          for (int i = 0; i < list.size(); i++) {
              //用一个临时变量temp来存储当前元素的值
              int temp = list.get(i);
      
              //计算temp里有几个x
              while (temp > 0) {
                  if (temp % 10 == x) {
                      count++;
                  }
                  temp = temp / 10;
              }
          }
          System.out.println(count);
      }
      

      }

      • 0
        @ 2025-2-25 16:50:03

        [NOIP 2013 普及组] 计数问题

        #include <iostream>
        using namespace std;
        
        int countDigit(int n,int x)
        {
          int count = 0;
          for(int base = 1;base<=n;base*=10)
          {
            int high = n/(base*10);
            int low = n%base;
            int curr = (n/base) % 10;
            if(x == 0){
              if(high>0)
              {
                high--;
              }
              else {
                break;
              }
            }
            count += high * base;
            if(curr == x)
            {
              count += low + 1;
            }
            else if(curr > x)
            {
              count += base;
            }
          }
          return count;
        }
        
        int main()
        {
          int n,x;
          cin>>n>>x;
          cout<< countDigit( n,  x) << endl;
          return 0;
        }
        
        • 1

        信息

        ID
        6033
        时间
        1000ms
        内存
        125MiB
        难度
        1
        标签
        递交数
        405
        已通过
        239
        上传者