首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 【微软面试题】请计算出1的个数 [已结帖,结帖人:lockhall]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lockhall
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 结帖率:
    发表于:2008-11-02 11:15:43 楼主
    原题目:
    给定一个十进制数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

    大家发散下,看看到底有多少种算法吧。注意复杂度O(∩_∩)O
    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Vegertar
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 11:19:231楼 得分:0
    UP~
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • aimeast
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 11:23:192楼 得分:0
    编程之美,一样的题目。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • aimeast
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 11:25:513楼 得分:1
    我这个程序是输出从1到N,所有的数字的个数。包括1的个数。但是这个程序现在计算0的个数是不正确的。现在确保思路没有问题。

    C/C++ code
    #include<iostream> #include<cmath> using namespace std; struct T{ int bit[10]; }; void plus(T& a,const T& b,const int k){ for(int i=0;i<10;i++) a.bit[i]+=b.bit[i]*k; } int main() { int i,j,k,r,w,z,n,mask; T a[10]; T ans; for(i=0;i<10;i++){ k=n=i-2>0?i-2:0; for(j=0;j<n;j++) k=k*10+8; if(i>1)a[i].bit[0]=k*10+9; else a[i].bit[0]=0; for(j=1;j<=9;j++) a[i].bit[j]=i*pow(10,i-1); } while(cin>>n && n){ for(i=0;i<10;i++)ans.bit[i]=0; mask=100000000;//10^9 i=9; while(n/mask==0){ mask/=10; i--; } z=0; while(n){ i--; k=n/mask; n=n%mask; mask/=10; if(k==0){ z++; continue; } if(z!=0){ if(mask==0)ans.bit[0]+=z*k; else ans.bit[0]+=z*k*mask*10+n; z=0; } plus(ans,a[i],k); for(j=1;j<k;j++) if(mask)ans.bit[j]+=mask*10-1; ans.bit[k]+=n; for(j=1;j<=k;j++) ans.bit[j]++; ans.bit[0]+=i*k; r=0; w=9; for(j=i-2;j>0;j--){ r+=w*j; w=w*10; } ans.bit[0]+=r*k; } for(i=0;i<10;i++) cout<<ans.bit[i]<<endl; cout<<endl; } return 0; }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lockhall
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 11:26:084楼 得分:0
    引用 2 楼 aimeast 的回复:
    编程之美,一样的题目。


    NOD

    那上面只是提供了个算法

    但是感觉还是有很多别的算法的;

    O(∩_∩)O
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • aimeast
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 11:26:575楼 得分:0
    如果你只需要计算1的个数,大可没有必要这么复杂。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • aimeast
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 11:33:466楼 得分:0
    这个是msra的原题
    http://www.msra.cn/Articles/ArticleItem.aspx?Guid=8ae08db5-e059-44bf-9181-83d40a67dadb
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zmlovelx
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:03:167楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • jia_xiaoxin
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:10:338楼 得分:1
    //效率最低的一种算法:O(n^2)
    C/C++ code
    #include <iostream> #include <string> int main() { int num = 0; cout << "Please input a dec num:" ; cin >> num; int total = 0; for(int i = 1; i <= num; i++) { int value = i; while(value > 0) { int single = value % 10; if(single == 1) total ++; value /= 10; } } cout << num << " include 1 total :" << total << endl; return 0; }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • hqin6
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:18:079楼 得分:1
    引用 6 楼 aimeast 的回复:
    这个是msra的原题
    http://www.msra.cn/Articles/ArticleItem.aspx?Guid=8ae08db5-e059-44bf-9181-83d40a67dadb


    不错不错~~~~
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lockhall
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:18:3110楼 得分:0
    引用 8 楼 jia_xiaoxin 的回复:
    //效率最低的一种算法:O(n^2)

    C/C++ code#include <iostream>
    #include <string>

    int main()
    {
        int num = 0;
        cout < < "Please input a dec num:" ;
        cin >> num;

        int total = 0;
        for(int i = 1; i <= num; i++)
        {
            int value = i;
           
            while(value > 0)
            {
                int single = value % 10;
                if(single == 1)
                    tota…


    还不算低,我还见过更低的;

    转换字符串,用字符串查找就是更慢!

    :)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • baihacker
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 4

    发表于:2008-11-02 12:21:5011楼 得分:1
    1.利用 x & (x-1)的性质
    2.利用位操作进行加法(有不同效率的实现)
    3.打表
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • lockhall
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:25:0312楼 得分:0
    引用 11 楼 baihacker 的回复:
    1.利用 x & (x-1)的性质
    2.利用位操作进行加法(有不同效率的实现)
    3.打表


    打表是什么意思啊?:)
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • baihacker
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 4

    发表于:2008-11-02 12:27:3313楼 得分:20
    引用 12 楼 lockhall 的回复:
    引用 11 楼 baihacker 的回复:
    1.利用 x & (x-1)的性质
    2.利用位操作进行加法(有不同效率的实现)
    3.打表


    打表是什么意思啊?:)


    C/C++ code
    char table16[16] = { 0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4} ; char table256[256]; unsigned int bitcount32_3(unsigned int x) { return table16[x&0xf]+table16[(x>>4)&0xf]+table16[(x>>8)&0xf]+table16[(x>>12)&0xf] + table16[(x>>16)&0xf]+table16[(x>>20)&0xf]+table16[(x>>24)&0xf]+table16[(x>>28)&0xf]; } unsigned int bitcount32_4(unsigned int x) { return table256[x&0xff] + table256[(x>>8)&0xff]+ table256[(x>>16)&0xff] + table256[(x>>24)&0xff]; } void SetData() { for (int i = 0; i < 16; i++) for (int j = 0; j < 16; j++) table256[i*16+j] = table16[i] + table16[j]; } unsigned int bitcount32_1(unsigned int x) { x = (x & 0x55555555UL) + ((x >> 1) & 0x55555555UL); x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL); x = (x & 0x0f0f0f0fUL) + ((x >> 4) & 0x0f0f0f0fUL); x = (x & 0x00ff00ffUL) + ((x >> 8) & 0x00ff00ffUL); x = (x & 0x0000ffffUL) + ((x >> 16) & 0x0000ffffUL); return x; } unsigned int bitcount32_2(unsigned int x) { x = x - ((x >> 1) & 0x55555555UL); x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL); x = (x + (x >> 4)) & 0x0f0f0f0fUL; x += x >> 8; x += x >> 16; return x & 0x3f; }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yzfyzyl
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:29:5214楼 得分:0
    最快的O(logn)算法,只喜欢那种
    动态规划算法



    飞燕算法群:46520219
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yzfyzyl
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-11-02 12:31:3015楼 得分:0
    baihacker 所说的位运算和本题无关



    飞燕算法群:46520219
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • baihacker
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 4

    发表于:2008-11-02 12:31:4116楼 得分:0
    囧.看错题目了.还以为是数一个32位数的1呢.
    我等会儿给你个和这个题目的要求类似的代码.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • baihacker
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 4

    发表于:2008-11-02 12:36:2417楼 得分:0
    能看懂多少就要看你的造化了

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

    C/C++ code
    我写的代码: #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; }



    C/C++ code
    //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