面试题

1. 原题目:
 给定一个十进制数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有"1"的个数。
 例如:
 N=2,写下1,2。这样只出现了1个"1"
 N=12,写下 1,2,3,4,5,6,7,8,9,10,11,12。这样"1"的个数是5
 请写出一个函数,返回1到N之间出现"1"的个数,比如 f(12)=5
来源:http://topic.csdn.net/u/20081102/11/1c6136bb-ad44-49e1-868b-d848816456f6.html

类似题源:

能看懂多少就要看你的造化了

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

#include    <iostream>

using namespace std;
const int MAX = 2008080808;
const int IT = 7;

template<int n, int curr, int last, int base, int weight>
class A
{
    enum{it = n/base%10};

public:
    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};

};


template<int n, int curr, int base, int weight>
class A<n, curr, n, base, weight>
{
public:
    enum {result = curr};

};

int main(int argc, char* argv[]) 

    cout << A<MAX+1, 0, 0, 1, 0>:: result << endl;
    return 0; 

//vitin的代码
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::system;

typedef unsigned __int64 num_type;
typedef unsigned short digit_type;

template <num_type base, size_t exp>
class power
{
public:

    static const num_type value = base * power<base, exp-1>::value;
};


template <num_type base>
class power<base, 0>
{
public:
    static const num_type value = 1;
};

 

template <num_type num, num_type base, bool bigger = (num >= base)>
class log_round 
{
public:
   static const size_t value = log_round<num/base, base>::value + 1;

};


template <num_type num, num_type base>
class log_round< num, base, false>
{
public:
    static const size_t value = 0;
};

template <num_type num, digit_type digit>
class count_of_num 
{
private:
    static const num_type base = 10;
    static const size_t exp = log_round<num, base>::value;
    static const num_type m_digit = num / power<base, exp>::value;
    static const num_type remain = num - m_digit * power<base, exp>::value;
    static const num_type m_digit_count = 
        (m_digit > digit) ? (power<base, exp>::value - count_of_num<power<base, exp>::value-1, digit>::value) :
        ((m_digit == digit) ? (remain+1-count_of_num<remain, digit>::value) : 0);

public:
    // 设 m_digit * (10^exp)  <= num < (m_digit+1)*(10^exp)
    // 则总数目包含以下几项
    // 1、m_digit * (1 到 (10^exp-1) 中的数目)
    // 2、1 到 (num - m_digit * (10^exp)) 中的数目
   // 3、最高位为 digit 的次数(去重)
    // 4、... digit == 0 比较复杂,暂不考虑
    static const num_type value = 
        m_digit * count_of_num<power<base, exp>::value-1, digit>::value
        + count_of_num<remain, digit>::value
        + m_digit_count;
};

template <digit_type digit>
class count_of_num <0, digit>
{
public:
    static const num_type value = 0;

};

int main()
{
    cout << count_of_num< 2008080808, 7 >::value << endl;

    system("pause");
    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值