zoukankan      html  css  js  c++  java
  • 715 QQ帐户的申请与登陆 (25分)

    这一题知识点与7-14基本上是一样的,都是用的散列。

    AC的代码:

    //将QQ号的类型从数字型改为字符串型后,成功AC。
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    long N;
    
    typedef struct user
    {
        char num[20];
        char pass[200];
        struct user* next;
    }node;
    typedef node* node_ptr;
    node_ptr* createtable(unsigned int size)
    {
        node_ptr* T = new node_ptr[N];
        for (unsigned int i = 0; i < size; i++)
        {
            T[i] = new node;
            T[i]->next = NULL;
        }
        return T;
    }
    typedef unsigned long long Index;
    Index hash_(char* key, unsigned int tablesize)
    {
        Index hash_val = 0;
        while (NULL != *(key+5))
        {
            hash_val = (hash_val << 5) + *(key++);
        }
        return hash_val % tablesize;
    }
    
    void apply(char* num, char* s2, node_ptr* T)
    {
        Index hash_val = hash_(num, N);
        node_ptr s = T[hash_val];
        while (NULL != s->next)
        {
            s = s->next;
            if (!strcmp(s->num,num))
            {
                cout << "ERROR: Exist" << endl;
                return;
            }
        }
        if (NULL == s->next)
        {
            node_ptr temp = new node;
            strcat(temp->num,num);
            strcpy(temp->pass, s2);
            temp->next = NULL;
            s->next = temp;
            cout << "New: OK" << endl;
            return;
        }
    }
    void login(char* num, char* s2, node_ptr* T)
    {
        Index hash_val = hash_(num, N);
        node_ptr s = T[hash_val];
        while (NULL != s->next)
        {
            s = s->next;
            if (!strcmp(s->num , num))
            {
                if (!strcmp(s->pass, s2))
                {
                    cout << "Login: OK" << endl;
                }
                else
                    cout << "ERROR: Wrong PW" << endl;
                return;
            }
        }
        if (NULL == s->next)
        {
            cout << "ERROR: Not Exist" << endl;
            return;
        }
    }
    int main()
    {
        cin >> N;
       char s1[20];
        char str2[200];
        node_ptr* T = createtable(N);
        char order;
        for (unsigned int i = 0; i < N; i++)
        {
            cin >> order;
            if (order == 'N')
            {
                cin >> s1;
                cin >> str2;
                apply(s1, str2, T);
            }
            else if (order == 'L')
            {
                cin >> s1;
                cin >> str2;
                login(s1, str2, T);
            }
        }
        return 0;
    }
  • 相关阅读:
    SQL Server 隐式转换引发的死锁
    C# List按某对象的属性分组 IGrouping
    C# 正则表达式获取json字符串中的键值
    .NET程序修改 ConfigurationManager 后,不需要重启IIS也可刷新Web.config配置文件
    相同结构的多个表合并到一个表的实现方法
    WCF系列_WCF影响客户端导出Excel文件的实现
    WCF系列_WCF如何选择不同的绑定
    WCF系列_WCF常用绑定选择
    JS生成URL二维码
    win 常用CMD命令备忘
  • 原文地址:https://www.cnblogs.com/2020R/p/Hash.html
Copyright © 2011-2022 走看看