fork download
  1. #include<iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5. int countValue(int number, int value) {
  6. int count =0;
  7. int curentIndex = 0;
  8. int currentValue = 0;
  9. int beforeValue = 0;
  10. int afterValue = 0;
  11. while(pow(10, curentIndex) < number) {
  12. currentValue = static_cast<int>(number/pow(10, curentIndex))%10;
  13. beforeValue = static_cast<int>(number/pow(10, (curentIndex + 1)));
  14. afterValue = number % static_cast<int>(pow(10, curentIndex));
  15.  
  16. if(currentValue == value) {
  17. count += beforeValue * pow(10, curentIndex) + afterValue + 1;
  18. } else if(currentValue < value) {
  19. count += beforeValue * pow(10, curentIndex);
  20. } else {
  21. count += (beforeValue + 1) * pow(10, curentIndex);
  22. }
  23. curentIndex++;
  24. }
  25. return count;
  26. }
  27.  
  28. int main()
  29. {
  30. int a, b;
  31. cout <<"Sample Input\n";
  32. cin >> a >> b;
  33. cout <<"Sample Output\n";
  34. cout << (countValue(b,3) + countValue(b,6) + countValue(b,9)
  35. - countValue((a-1),3) - countValue((a-1),6) - countValue((a-1),9));
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5328KB
stdin
999999
10000000
stdout
Sample Input
Sample Output
19200006