• 常见面试题学习(1)


    1. 反转字符串。

    #include <iostream>

    using namespace std;
    /*
    * 编写反转字符串的程序,要求优化速度、优化空间。
    */
    void reverse(char *str,int len)
    {
    char *p = str;
    char *q = str + len - 1;
    len
    /= 2;
    while(len > 0)
    {
    *p = *p ^ *q;
    *q = *p ^ *q;
    *p = *p ^ *q;
    p
    ++;
    q
    --;
    len
    --;
    }
    }
    int main()
    {
    char str[11] = "9876543210";
    reverse(str,
    sizeof(str) - 1);

    cout
    << str << endl;
    return 0;
    }

      2. 判断是不是循环链表,用两个指针,一个每次前进一步,一个每次前进2步,看他们的结果是否相同

    #include <iostream>
    #include
    <stdio.h>
    #include
    <stdlib.h>
    using namespace std;
    struct node
    {
    int key;
    node
    *next;
    };
    node
    * head;
    void create()
    {
    int i;
    node
    *p = NULL;
    head
    = (node *)malloc(sizeof(node));
    head
    ->key = 0;
    head
    ->next = NULL;

    p
    = head;

    for(int i = 0;i < 6;i ++)
    {
    p
    ->next = (node *)malloc(sizeof(node));
    p
    ->next->key = i;
    p
    ->next->next = NULL;
    p
    = p->next;
    }
    p
    ->next = head;//使尾节点指向头节点

    #ifdef TEST
    p
    = head;
    for(int i = 0;i < 12;i ++)
    cout
    << p->key << " ";
    cout
    << endl;
    #endif

    }

    void test()
    {
    node
    *p = NULL;
    node
    *pp = NULL;
    p
    = head->next;
    pp
    = head->next->next;
    while(pp != p)
    {
    p
    = p->next;
    pp
    = pp->next->next;
    if(p == NULL || pp == NULL)
    {
    cout
    << "不是循环链表" << endl;
    return ;
    }
    }
    cout
    << "是循环链表" << endl;

    }

    void des()
    {
    node
    *p = NULL,*q = NULL;
    p
    = head->next;
    while(p != head)
    {
    q
    = p;
    p
    = p->next;
    free(q);
    }
    free(head);
    }
    int main()
    {
    create();
    test();
    des();
    return 0;
    }

      3. 将 一个字符串型的数字转化为long 型

       记得上次百度有一道面试题是这样的,将一个字符串转化为long ,但是字符串中可能包含小数点,和其他字符,问的是能不能也向下面的方式,一遍遍历求出结果,要求是写

       出代码,而不是说就行

    #include <iostream>
    #include
    <stdio.h>
    #include
    <string.h>
    using namespace::std;

    const int MAX = 10;

    int my_power(int n)
    {
    if(n <= 0)
    return 1;
    int temp = 1;
    while (n--)
    temp
    *= 10;
    return temp;
    }

    float convert(const char* str)
    {
    float sum1 = 0.0;
    float sum2 = 0.0;
    int i = 0;
    int idx = 0;
    int len = strlen(str);

    while(str[idx] != '.' && str[idx] != '\0')
    idx
    ++;

    while(i < idx)
    {
    sum1
    = sum1 * 10 + (str[i]-'0'); //整数部分
    i ++;
    }
    i
    = idx + 1;
    if(i < len)
    {
    while(i < len)
    {
    sum2
    = sum2 * 10 + (str[i]-'0');
    i
    ++;
    }
    }


    return (sum1+sum2/my_power(len - idx - 1));
    }
    int main()
    {
    float result = 0.0;
    char str[MAX];
    cin
    >> str;
    result
    = convert(str);
    cout
    << result << endl;
    return 0;
    }

      

    #include <iostream>

    using namespace std;

    /*
    写一个函数,检查字符串是否是整数,如果是,返回其整数值。
    */

    long convert(const char * str)
    {
    long value = 0,f = 1;
    if(* str == '-') str ++,f = -1;
    for(; *str != '\0' && *str >= '0' && *str <= '9'; str ++) value = value * 10 + (*str - '0');

    return *str == '\0' ? value * f:0;
    }
    int main()
    {
    char str[] = "123456789";
    cout
    << convert(str) << endl;

    char str3[] = "-1234567";
    cout
    << convert(str3) << endl;

    return 0;
    }

      百度那道题:考虑各种情况下。

    #include <iostream>
    #include
    <iomanip>
    using namespace std;
    inline
    bool IsNum(const char c)
    {
    return c >= '0' && c <= '9';
    }

    inline
    bool IsAlpha(const char c)
    {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }


    // 从字符串str中获得一个浮点数
    // start表示这个浮点数在字符串中的起始位置。end表示浮点数结束的下一个位置
    // dot表示小数点所在的位置
    bool CheckString(const char* str, int& start, int& dot, int& end)
    {
    if (str == NULL)//字符串为空
    return false;

    char* p = const_cast<char*>(str);

    while(!IsNum(*p++));
    p
    --;
    start
    = p - str;

    if (str[start - 1] == '.')// -.14f
    {
    start
    --; p--;
    }

    for (; IsNum(*p); p++);

    if (*p == '.')
    {
    dot
    = p - str;
    while(IsNum(*++p));
    end
    = p - str;
    return true;
    }

    dot
    = p - str;
    end
    = p - str;
    return true;
    }


    //a^b
    double Pow(const int a, const int b)
    {
    double rslt = 1;
    for (int i = 0; i < b; i++)
    rslt
    *= a;

    return rslt;
    }
    // 把一个字符串转化成double型的浮点数
    double Atof(const char* str)
    {
    int s, d, e;
    if (!CheckString(str, s, d, e))
    {
    cout
    << "please check your string." <<endl;
    return 0;
    }
    double result = 0.0;

    double pow = Pow(10, e - 1 - d);

    // 计算小数部分
    for (int i = e - 1; i > d; i--)
    {
    result
    += (str[i] - '0') / pow;
    pow
    /= 10;
    }

    pow
    = 1;
    // 计算整数部分
    for (int j = d - 1; j >= s; j--)
    {
    result
    += (str[j] - '0') * pow;
    pow
    *= 10;
    }

    if (s > 0 && str[s - 1] == '-')
    result
    = -result;

    return result;
    }
    int main()
    {
    cout
    << endl;
    cout
    << endl << setprecision(10) << Atof("-123.456") << endl;
    return 0;
    }

     

  • 相关阅读:
    活期存款利息的计算方法
    Arachni web扫描工具
    python 多进程——使用进程池,多进程消费的数据)是一个队列的时候,他会自动去队列里依次取数据
    TCP报文格式和三次握手——三次握手三个tcp包(header+data),此外,TCP 报文段中的数据部分是可选的,在一个连接建立和一个连接终止时,双方交换的报文段仅有 TCP 首部。
    https ddos检测——研究现状
    https ddos攻击——由于有了认证和加解密 后果更严重 看绿盟的产品目前对于https的ddos cc攻击需要基于内容做检测
    识别TLS加密恶意流量
    CC工具列表
    MAP 最大后验——利用经验数据获得对未观测量的点态估计
    MAPE 平均绝对百分误差
  • 原文地址:https://www.cnblogs.com/hitwtx/p/2152120.html
Copyright © 2020-2023  润新知