金山招聘题目:编程计算从1到2008080808之间的整数有多少个含有数字7

  1. 我的代码(如果要加一个模板参数来判断n和last是否相等,会有意想不到的效果):
  2. #include    <iostream>
  3. using namespace std;
  4. const int MAX = 2008080808;
  5. const int IT = 7;
  6. template<int n, int curr, int last, int base, int weight>
  7. class A
  8. {
  9.     enum{it = n/base%10};
  10. public:
  11.     enum{result = A<n, (it-(it>IT))* weight+((it<IT)?curr:(it==IT?last:base+curr)), last+base*it, base*10, (weight?weight*9+base:1)> :: result};
  12. };
  13. template<int n, int curr, int base, int weight>
  14. class A<n, curr, n, base, weight>
  15. {
  16. public:
  17.     enum {result = curr};
  18. };
  19. int main(int argc, char* argv[]) 
  20.     cout << A<MAX+1, 0, 0, 1, 0>:: result << endl;
  21.     return 0; 
  22. 下面的vitin的代码,思路一样,只是方向相反#include <iostream>
  23. #include <cstdlib>
  24. using std::cout;
  25. using std::endl;
  26. using std::system;
  27. typedef unsigned __int64 num_type;
  28. typedef unsigned short digit_type;
  29. template <num_type base, size_t exp>
  30. class power
  31. {
  32. public:
  33.     static const num_type value = base * power<base, exp-1>::value;
  34. };
  35. template <num_type base>
  36. class power<base, 0>
  37. {
  38. public:
  39.     static const num_type value = 1;
  40. };
  41. template <num_type num, num_type base, bool bigger = (num >= base)>
  42. class log_round 
  43. {
  44. public:
  45.    static const size_t value = log_round<num/base, base>::value + 1;
  46. };
  47. template <num_type num, num_type base>
  48. class log_round< num, base, false>
  49. {
  50. public:
  51.     static const size_t value = 0;
  52. };
  53. template <num_type num, digit_type digit>
  54. class count_of_num 
  55. {
  56. private:
  57.     static const num_type base = 10;
  58.     static const size_t exp = log_round<num, base>::value;
  59.     static const num_type m_digit = num / power<base, exp>::value;
  60.     static const num_type remain = num - m_digit * power<base, exp>::value;
  61.     static const num_type m_digit_count = 
  62.         (m_digit > digit) ? (power<base, exp>::value - count_of_num<power<base, exp>::value-1, digit>::value) :
  63.         ((m_digit == digit) ? (remain+1-count_of_num<remain, digit>::value) : 0);
  64. public:
  65.     // 设 m_digit * (10^exp)  <= num < (m_digit+1)*(10^exp)
  66.     // 则总数目包含以下几项
  67.     // 1、m_digit * (1 到 (10^exp-1) 中的数目)
  68.     // 2、1 到 (num - m_digit * (10^exp)) 中的数目
  69.    // 3、最高位为 digit 的次数(去重)
  70.     // 4、... digit == 0 比较复杂,暂不考虑
  71.     static const num_type value = 
  72.         m_digit * count_of_num<power<base, exp>::value-1, digit>::value
  73.         + count_of_num<remain, digit>::value
  74.         + m_digit_count;
  75. };
  76. template <digit_type digit>
  77. class count_of_num <0, digit>
  78. {
  79. public:
  80.     static const num_type value = 0;
  81. };
  82. int main()
  83. {
  84.     cout << count_of_num< 2008080808, 7 >::value << endl;
  85.     system("pause");
  86.     return 0;
  87. }
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值