zoukankan      html  css  js  c++  java
  • 随机生成字符串

    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    struct Node {
        double val;
        int ran;
        char ch;
        bool operator < (const Node & rhs) const {
            if (val != rhs.val) return val < rhs.val;
            else return ran > rhs.ran;
        }
        Node(double _val, int _ran, char _ch) {
            val = _val, ran = _ran, ch = _ch;
        }
    };
    
    priority_queue<Node> que;
    const int maxn = RAND_MAX + 20;
    bool vis[maxn];
    map<unsigned long long int, bool> mp;
    char str[22222];
    const int seed = 131;
    
    void show() {
        static int f = 0;
        if (f < 100) printf("f
    ");
        else exit(-1);
        f++;
    }
    
    void work() {
        srand(time(NULL));
        for (int i = 'a'; i <= 'z'; ++i) {
            int ran = rand();
            while (vis[ran]) ran = rand();
            vis[ran] = true;
            que.push(Node(1e18, ran, i));
        }
        for (int i = '0'; i <= '9'; ++i) {
            int ran = rand();
            while (vis[ran]) ran = rand();
            vis[ran] = true;
            que.push(Node(1e18, ran, i));
        }
        int ansNum, ansLen;
        printf("please enter the number for Code : ");
        scanf("%d", &ansNum);
        printf("please enter the len for Code : ");
        scanf("%d", &ansLen);
        for (int i = 1; i <= ansNum; ++i) {
            unsigned long long int hashVal = 0;
            for (int j = 1; j <= ansLen; ++j) {
                Node t = que.top();
                que.pop();
                str[j] = t.ch;
                t.val /= 2;
                vis[t.ran] = false;
                int en = rand();
                while (vis[en] == true) {
    //                show();
                    en = rand();
    //                printf("%d
    ", en);
                }
                t.ran = en;
                que.push(t);
                hashVal = hashVal * seed + t.ch;
            }
            if (!mp[hashVal]) {
                mp[hashVal] = true;
                printf("%s
    ", str + 1);
            } else {
                i--;
            }
        }
    }
    
    int main() {
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    哈哈,拖了一天
    内置函数续
    内置函数2
    内置函数1
    内置函数补充
    生成器面试题
    字段值为null的处理
    oracle日期转换报ORA-01810: 格式代码出现两次 01810. 00000 -  "format code appears twice"
    通过sql脚本可以从数据库中查到数据,但是通过jdbc却获取不到
    java外部捕获了异常,但是并没有打印异常和将异常信息写入日志文件
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/7698788.html
Copyright © 2011-2022 走看看