如何在不加法的情况下将数字绑定在一起成为一个整数?

How to bind numbers together into an integer without addition?

例如: 如果我最终得到多位数字并且所有这些数字都是随机生成的 (1-9) 总共最多 7 位数字并且我希望它们最终都是一个整数,我该怎么做呢?

这是我试过的东西:

static void generate_key(std::map<std::string, int>& key, std::string c_user)
{
    unsigned int hold[7]{}; // will hold all the random integers seperate to add onto final
    int *final = new int; // will store a complete 7 digit integer here later on

    for (int i = 0; i < 7; i++)
    {
        unsigned int num = rand() % 9;
        hold[i] = num;
    }

    *final = hold[0] + hold[1] + hold[2] + hold[3]; // I know this sums everything but how do I get it to just add all the numbers into a single integer?!
    key.emplace(c_user, final);

    std::cout << "Key created: " << *final << '\n';

    delete final;
}

将所有整数彼此分开:

unsigned int hold[7]{};

创建随机数字:

for (int i = 0; i < 7; i++)
        {
            unsigned int num = rand() % 9;
            hold[i] = num;
        }

将所有整数存储并添加到*final整数

int *final = new int;

我想生成一个 7 位数字的密钥,每个数字都是随机的。然后我想将它们全部相加以构成一个完整的整数,该整数被放入一个映射中,其中已经创建了一个字符串并且该键将代表该字符串。你明白我在这里说的吗?对不起,如果我的解释很糟糕,我在这里太迷路了..

如果有帮助的话,这里是完整的代码:

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stdlib.h>

class Account
{
private:
    std::string username;
    std::string password;
public:
    Account(std::string username, std::string password)
    {
        this->username = username;
        this->password = password;
    }
    ~Account() {};

    std::string get_username()const { return username; }
    std::string get_password()const { return password; }
};

static void create_account(std::string user, std::string pass, std::vector<Account> &acc)
{
    Account *account = new Account(user, pass);

    acc.push_back(*account);

    delete account;
}

static void generate_key(std::map<std::string, int>& key, std::string c_user)
{
    unsigned int hold[7]{};
    int *final = new int;

    for (int i = 0; i < 7; i++)
    {
        unsigned int num = rand() % 9;
        hold[i] = num;
    }

    *final = hold[0] + hold[1] + hold[2] + hold[3];

    key.emplace(c_user, final);

    std::cout << "Key created: " << *final << '\n';

    delete final;
}

std::vector<Account> accounts;
std::map<std::string, int> keys;

int main()
{
    srand(time(NULL));

    create_account("random", "triathlon", accounts);
    generate_key(keys, accounts[0].get_username());

    return 0;
}

static void create_account(std::string, std::string, std::vector<Account> &acc);
static void generate_key(std::map<std::string, int>&, std::string);

如果你想生成一个所有数字都是唯一的 7 位数字,那么你可以将有效数字存储在 vector 中,然后 shuffle 该向量并取前 7元素作为组成数字的数字。可以这样做:

std::vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
std::random_device rd;
std::mt19937 g(rd());
std::suffle(digits.being(), digits.end(), g);
std::string number{digits.begin(), digits.begin() + 7}

现在 number 是一个 7 位数字的字符串,其中每个数字只出现一次。如果你不想有前导零,那么你需要检查第一个元素是否为零,如果是,再次随机播放或只取元素 1 到 7。

如果你只想生成一个 7 位数字,但不关心是否有重复数字,那么你可以使用 std::string 和一个简单的 for 循环来实现,例如:

std::random_device rd;
std::mt19937 g(rd());
std::uniform_int_distribution<> dist(0, 9);
std::string number;
for (int i = 0; i < 7; ++i)
{
    number.push_back('0' + dist(g))
}

如果您只想要一个具有唯一数字的随机七位数字,您可以使用典型的随机随机播放算法直接生成它,在该算法中,您可以在数组中生成一个随机索引,取该项目,交换数组的背面,并弹出数组。

忘记这个算法叫什么了

在您的情况下,您只需要从九位数字开始进行七次迭代。代码如下:

#include <vector>
#include <array>
#include <algorithm>
#include <iostream>

int remove_rnd_item( std::vector<int>& digits ) {
    int n = rand() % digits.size(); // should use a better RNG...
    int rnd_digit = digits[n];
    //swap with the last digit and pop...
    std::swap(digits[n], digits.back());
    digits.pop_back();
    return rnd_digit;
}

int random_seven_digit_number_with_unique_digits() {
    std::vector<int> digits = { 0,1,2,3,4,5,6,7,8,9 };
    std::array<int, 7> rnd_digits;
    std::generate(rnd_digits.begin(), rnd_digits.end(),
        [&digits]() {
            return remove_rnd_item(digits);
        }
    );
    // convert the digits vector to a single int, if that is what you want
    int tens_place = 1;
    int val = 0;
    for (auto digit : rnd_digits) {
        val += tens_place * digit;
        tens_place *= 10;
    }
    return val;
}

int main() {
    for (int i = 0; i < 10; ++i) {
        std::cout << random_seven_digit_number_with_unique_digits() << "\n";
    }
}

我认为在客户 ID 中包含所有唯一数字的要求是错误的。

要获得一个随机的 7 位数号码,您只需要:

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> distrib(0, 9'999'999);

    std::cout << distrib(gen) << std::endl;
}

如果要避免前导零,请使用 distrib(1'000'000, 9'999'999);

哇哦!嗨!我也是初学者!是的。我想你可以这样做!

int boop(){
    int ding=1000000;
    int a[7]={1,4,6,3,4,2,4};
    int re=0;
    for(int i=0;i<7;i++){
        re+=a[i]*ding;
        ding/=10;
    }
    return re;
}

或者自己编写一个函数,生成一个 0 到 1 之间的随机数,然后乘以 9999999。(有点推荐)。

#include <iostream>
#include <math.h>
float boop(int seed){
    return abs(sin( cos(seed*3972)*38472));
}
int main(){
    float f=boop(34)*9999999;
    std::cout<<(int)f;
    return 0;
}