top1编程
← 返回题目
题解

【基础】数的统计

3 条题解

  • 0
    @ 2026-7-29 20:23:10
    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	int n, x, cnt = 0;//1~n之间x这个数出现了多少次,cnt计数器
    	cin >> n >> x;
    	//扫描1~n之间的所有数
    	for (int i = 1; i <= n; i++) {
    		//对i扫描到的数进行拆位
    		int t = i;//不可以直接拆i会影响循环
    		while (t != 0) {//一直拆,拆到t == 0
    			if (t % 10 == x) {//找到和x一样的数就将计数器+1
    				cnt++;
    			}
    			t /= 10;//千万别忘了除以10,否则无限循环
    		}
    	}
    	cout << cnt;
    	return 0;
    }
    
    • 0
      @ 2026-7-29 0:06:22
      #include<bits/stdc++.h>
      using namespace std;
      int a[10];
      int main() {
      	int n, x;
      	cin >> n >> x;
      	for (int i = 1; i <= n; i++) {
      		int t = i;
      		while (t != 0) {
      			if (t % 10 == x) {
      				a[x]++;
      			}
      			t /= 10;
      			
      		}
      	}
      	cout << a[x];
      	return 0;
      }
      
      • 0
        @ 2026-7-28 22:09:27
        #include<bits/stdc++.h>
        using namespace std;
        int n,x,a[10];
        int main() {
        	cin>>n>>x;
        	for(int i=1;i<=n;i++){
        		int r=i;
        		while(r!=0){
        			a[r%10]++;
        			r/=10;
        		}
        	}
        	cout<<a[x];
        	return 0;
        }
        
        • 1