zoukankan      html  css  js  c++  java
  • 腾讯面试第六题!

    第6题
    ------------------------------------
    腾讯面试题:
    给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
    要求下排每个数都是先前上排那十个数在下排出现的次数。
    上排的十个数如下:
    【0,1,2,3,4,5,6,7,8,9】

    初看此题,貌似很难,10分钟过去了,可能有的人,题目都还没看懂。

    举一个例子,
    数值: 0,1,2,3,4,5,6,7,8,9
    分配: 6,2,1,0,0,0,1,0,0,0
    0在下排出现了6次,1在下排出现了2次,
    2在下排出现了1次,3在下排出现了0次....
    以此类推..
    // 引用自July 2010年10月18日。

    //数值: 0,1,2,3,4,5,6,7,8,9
    //分配: 6,2,1,0,0,0,1,0,0,0

    #include <iostream.h>
    #define len 10

    class NumberTB
    {
    private:
    int top[len];
    int bottom[len];
    bool success;
    public:
    NumberTB();
    int* getBottom();
    void setNextBottom();
    int getFrequecy(int num);
    };

    NumberTB::NumberTB()
    {
    success = false;
    //format top
    for(int i=0;i<len;i++)
    {
    top[i] = i;
    }
    }


    int* NumberTB::getBottom()
    {
    int i = 0;
    while(!success)
    {
    i++;
    setNextBottom();
    }
    return bottom;
    }

    //set next bottom
    void NumberTB::setNextBottom()
    {
    bool reB = true;

    for(int i=0;i<len;i++)
    {
    int frequecy = getFrequecy(i);

    if(bottom[i] != frequecy)
    {
    bottom[i] = frequecy;
    reB = false;
    }
    }
    success = reB;
    }

    //get frequency in bottom
    int NumberTB::getFrequecy(int num) //此处的num即指上排的数 i
    {
    int count = 0;

    for(int i=0;i<len;i++)
    {
    if(bottom[i] == num)
    count++;
    }
    return count; //cout即对应 frequecy
    }

    int main()
    {
    NumberTB nTB;
    int* result= nTB.getBottom();

    for(int i=0;i<len;i++)
    {
    cout<<*result++<<endl;
    }
    return 0;
    }
    ///////////////////////////////////////////
    运行结果:
    6
    2
    1
    0
    0
    0
    1
    0
    0
    0
    Press any key to continue
    /////////////////////////////////////////

  • 相关阅读:
    Windows下Yarn安装与使用
    Node.js安装及环境配置之Windows篇
    sharding-jdbc—分片策略:Inline行表达式分片策略InlineShardingStrategy(2)
    sharding-jdbc—分片策略:标准分片策略StandardShardingStrategy(1)
    sharding-jdbc—分片策略(总)
    ShardingJdbc 数据脱敏
    ShardingJdbc 数据分布式事务
    Spring Boot整合Sharding-JDBC实现分库分表+读写分离org.apache.shardingsphere+mybatis-plus(4)
    jenkins docker
    nps 使用
  • 原文地址:https://www.cnblogs.com/acgpiano/p/4191824.html
Copyright © 2011-2022 走看看