pages:
  • 1
Seyed Hamid SAYS

i've got timelimit, how i can optimize my code ?

#include <iostream>
#include <cmath>

using namespace std;

int numlength(unsigned int n) {
int x = 0;
do {
    ++x;
    n /= 10;
} while (n);
return x;
}

int main() {

unsigned int start, end;

while (cin >> start >> end) {

    unsigned int num[10] = { 0 };

    if ((start == 0) && (end == 0))
        return 0;

    for (unsigned int i = start; i <= end; i++) {
        for (short int j = 0; j < numlength(i); j++) {
            int x = pow(10, j);
            int y = (i / x) % 10;
            num[y] += 1;
        }
    }

    for (int n = 0; n <= 9; n++)
        cout << num[n] << " ";
    cout << endl;
}

return 0;
}