Cod sursa(job #2091257)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 19 decembrie 2017 13:45:33
Problema Cifre Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <bits/stdc++.h>

int D(int n, int c, int k){
    if(n < 10){
        if(k > 1) return 0;
        else if(k == 1){
            if(n >= c) return 1;
            return 0;
        }
        else return n + 1;
    }
    int cn = n, p10 = 1;
    while(cn > 0){
        p10 *= 10;
        cn /= 10;
    }
    p10 /= 10;
    int lastcif = n / p10;
    int ans = 0;
    if(c != 0){
        if(lastcif == c)
            ans += D(n % p10, c, k - 1);
        else
            ans += D(n % p10, c, k);
        lastcif--;
        if(lastcif < c)
            ans += (lastcif + 1) * D(p10 - 1, c, k);
        else
            ans += lastcif * D(p10 - 1, c, k) + D(p10 - 1, c, k - 1);
        return ans;
    }
    else{
        ans += D(n % p10, c, k);
        ans += lastcif * D(p10 - 1, c, k);
        return ans;
    }
}
int main(){
    FILE*fi,*fo;
    fi=fopen("cifre.in","r");
    fo=fopen("cifre.out","w");

    int a, b, c, k;
    fscanf(fi,"%d%d%d%d", &a, &b, &c, &k);
    fprintf(fo,"%lf", (1.0 * D(b, c, k) - 1.0 * D(a - 1, c, k)) / (1.0 * b - 1.0 * a + 1));
    fclose(fi);
    fclose(fo);
    return 0;
}