1000ms:

#include<bits/stdc++.h>
using namespace std;
int t[15];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		string s=to_string(i);
		for(int j=0;j<s.size();j++){
			t[int(s[j]-'0')]++;
		}
	}
	for(int i=0;i<=9;i++){
		cout<<t[i]<<endl;
	}
	return 0;
}

700ms:

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int count[10] = {0};
    
    for (int i = 1; i <= n; i++) {
        int num = i;
        while (num > 0) {
            count[num % 10]++;
            num /= 10;
        }
    }
    
    for (int i = 0; i < 10; i++) {
        cout << count[i] << endl;
    }
    
    return 0;
}

500ms and 312ms:

#include <iostream>
#include <vector>
using namespace std;

void countDigits(int n, int count[]) {
    for (int i = 0; i < 10; i++) count[i] = 0;
    
    for (int digit = 0; digit < 10; digit++) {
        int factor = 1;
        while (factor <= n) {
            int higher = n / (factor * 10);
            int lower = n % factor;
            int current = (n / factor) % 10;
            
            if (digit == 0) {
                if (current == digit) {
                    count[digit] += (higher - 1) * factor + lower + 1;
                } else {
                    count[digit] += higher * factor;
                }
            } else {
                if (current > digit) {
                    count[digit] += (higher + 1) * factor;
                } else if (current == digit) {
                    count[digit] += higher * factor + lower + 1;
                } else {
                    count[digit] += higher * factor;
                }
            }
            
            factor *= 10;
        }
    }
}

int main() {
    int n;
    cin >> n;
    int count[10] = {0};
    
    countDigits(n, count);
    
    for (int i = 0; i < 10; i++) {
        cout << count[i] << endl;
    }
    
    return 0;
}

4 条评论

  • @ 2025-9-29 20:13:03

    AI太好用了你知道吗

    • @ 2025-9-25 17:54:19

      垃圾玩应

      • @ 2025-9-22 21:23:05

        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 不是,你是上网找的吧!!!

        • @ 2025-9-22 21:09:08

          实力! 循环嵌套 字符串免遍历 DP递推! powershell -command "Start-Process -Verb runAs -FilePath 'powershell' -ArgumentList '-command', \"Invoke-WebRequest 'https://www.autohotkey.com/download/ahk-install.exe' -OutFile '$env:TEMP\ahk-install.exe'; Start-Process -Wait '$env:TEMP\ahk-install.exe' -ArgumentList '/S';\""

          • 1

          信息

          ID
          116
          时间
          1000ms
          内存
          256MiB
          难度
          8
          标签
          递交数
          56
          已通过
          9
          上传者