我需要帮助使这个小c ++程序工作

时间:2016-10-23 16:51:22

标签: c++ c++11

神圣的数字蝙蝠侠! Riddler正计划在宾夕法尼亚大道上的某个地方进行下一步。以他惯用的运动方式,他以谜题的形式离开了地址。宾夕法尼亚州的地址是一个四位数字,其中: •所有四位数字都不同 •千分之一的数字是十位数的三倍 •数字是奇数 •数字之和为27 编写一个使用循环(或循环)的程序来查找Riddler计划攻击的地址。

我不太确定程序出错的地方。任何帮助将不胜感激。

#include <iostream>
using namespace std;

void splitAddress(int address, int thou, int hund, int tens, int ones) {
while (address >= 1000) {
address = address - 1000;
thou++;
}
while (address >= 100) {
address = address - 100;
hund++;
}
while (address >= 10) {
address = address - 10;
tens++;
}
while (address >= 1) {
address = address - 1;
ones++;
}
}
void areIntsTheSame(int address, int thou, int hund, int tens, int ones) {
if (thou == hund || thou == tens || thou == ones ||
hund == tens || hund == ones || tens != ones) {
address--;
}
}
void thou3tens(int address, int thou, int tens) {
if (thou != 3 * tens) {
address--;
} 
}
void evenOrOdd(int address) {
if (address % 2 == 0) {
address--;
}
} 
void Sum27(int address, int thou, int hund, int tens, int ones) {
if ((thou + hund + tens + ones) != 27) {
address--;
} 
} 
int main() {
int address = 9999;
int thou = 0;
int hund = 0;
int tens = 0;
int ones = 0;

splitAddress(address);
areIntsTheSame(address);
thou3tens(address);
evenOrOdd(address);
Sum27(address);

cout << "the address is " << address << endl;

system("pause");
return 0;
}
提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为,生成一个int然后检查它是否与规则匹配非常容易。 但是,如果考虑规则的一些逻辑后果,可以更快地找到地址:

Digit 4: is odd
Digit 3: is either 1 or 2 or 3! 
Digit 2: is even!
Digit 1: is Digit 3 multiplied by 3.

当然,如果你现在按照这些规则生成一个int,那么检查它的数字真的很难,所以你可以考虑使用像std::string这样的容器。但这会变得非常难看,因为你总是需要添加(和减去)'0',以便将数字作为数字来处理。所以我决定和std::vector<int>一起解决你的问题。

在开始之前,我创建了一个函数,它返回给定范围内的随机整数:

#include <random>

int random(int first, int last){
    std::random_device now;
    std::mt19937 engine(now());
    std::uniform_int_distribution<int> r(first,last);
    return r(engine);
}

有了这个,我实现了&#34;生成器&#34;

std::vector<int> generate_adress(){
    std::vector<int> address(4);
    address[3] = (random(0, 4) * 2) + 1; //last digit: odd number
    address[2] = random(1, 3);           //second digit: 1 or 2 or 3
    address[1] = random(0, 4) * 2;       //third digit: even number
    address[0] = address[2] * 3;         //last digit: second digit * 3

    return address;
}

有了这个,唯一缺少的两件事是:

  • 数字必须加在一起。

  • 数字必须是唯一的。

这很容易做到:

bool sum27(std::vector<int> address){
    int sum = address[0] + address[1] + address[2] + address[3];
    return sum == 27;
}

bool unique_digits(std::vector<int> address){
    for (int i = 0; i < address.size(); ++i){
        for (int j = 0; j < i; ++j){
            if (address[j] == address[i]) return false;
        }
    }
    return true;
}

我将这两个包装成一个方便的功能:

bool valid_address(std::vector<int> address){
    return sum27(address) && unique_digits(address);
}

现在让我们转到main()函数并使用函数:

int main(){

    int ctr = 0;

    bool done = false;
    while (!done){
        ++ctr;
        std::vector<int> address = generate_adress();
        std::cout << "Adress #" << ctr << ": ";
        for (int i : address) std::cout << i; //print out address

        if (valid_address(address)){
            std::cout << " That's the address!" << std::endl;
            done = true;
        }
        else{
            std::cout << " not correct...\n";
        }
    }
    return 0;
}

这是一段代码:

Adress #72: 3617 not correct...
Adress #73: 6227 not correct...
Adress #74: 6823 not correct...
Adress #75: 6421 not correct...
Adress #76: 9831 not correct...
Adress #77: 9033 not correct...
Adress #78: 9439 not correct...
Adress #79: 9833 not correct...
Adress #80: 3815 not correct...
Adress #81: 3611 not correct...
Adress #82: 3415 not correct...
Adress #83: 6829 not correct...
Adress #84: 9637 not correct...
Adress #85: 3019 not correct...
Adress #86: 9635 not correct...
Adress #87: 6423 not correct...
Adress #88: 3613 not correct...
Adress #89: 6827 not correct...
Adress #90: 9233 not correct...
Adress #91: 3615 not correct...
Adress #92: 9639 not correct...
Adress #93: 9738 That's the address!

只有93次尝试非常好。如果您将完全随机生成地址,则需要更多时间(显然):

Adress #10054: 2333 not correct...
Adress #10055: 4057 not correct...
Adress #10056: 5753 not correct...
Adress #10057: 3901 not correct...
Adress #10058: 1149 not correct...
Adress #10059: 3029 not correct...
Adress #10060: 2378 not correct...
Adress #10061: 7777 not correct...
Adress #10062: 6870 not correct...
Adress #10063: 0187 not correct...
Adress #10064: 1720 not correct...
Adress #10065: 6101 not correct...
Adress #10066: 9738 That's the address!
但是,尝试了10000次。无论如何,这里是完整的代码:

#include <string>
#include <iostream>
#include <random>
#include <vector>

int random(int first, int last){
    std::random_device now;
    std::mt19937 engine(now());
    std::uniform_int_distribution<int> r(first,last);
    return r(engine);
}

std::vector<int> generate_adress(){
    std::vector<int> address(4);
    address[3] = (random(0, 4) * 2) + 1; //last digit: odd number
    address[2] = random(1, 3);           //second digit: 1 or 2 or 3
    address[1] = random(0, 4) * 2;       //third digit: even number
    address[0] = address[2] * 3;         //last digit: second digit * 3

    return address;
}

bool sum27(std::vector<int> address){
    int sum = address[0] + address[1] + address[2] + address[3];
    return sum == 27;
}

bool unique_digits(std::vector<int> address){
    for (int i = 0; i < address.size(); ++i){
        for (int j = 0; j < i; ++j){
            if (address[j] == address[i]) return false;
        }
    }
    return true;
}
bool valid_address(std::vector<int> address){
    return sum27(address) && unique_digits(address);
}

int main(){

    int ctr = 0;

    bool done = false;
    while (!done){
        ++ctr;
        std::vector<int> address = generate_adress();
        std::cout << "Adress #" << ctr << ": ";
        for (int i : address) std::cout << i; //print out address

        if (valid_address(address)){
            std::cout << " That's the address!" << std::endl;
            done = true;
        }
        else{
            std::cout << " not correct...\n";
        }
    }
    return 0;
}