为什么我的代码首先执行后来的cout?

时间:2015-01-07 01:40:58

标签: c++

以下代码

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

bool checkPerm(unsigned long long x){
    vector<unsigned long long> tester;
    string strx = to_string(x);
    int sizestrx = strx.size();
    int counter = 1;
    cout << "x is " << strx << " and its permutations are ";
    while (next_permutation(strx.begin(), strx.end())){
        cout << strx << " ";
        unsigned long long stoipermstrx = stoi(strx);
        tester.push_back(stoipermstrx);
    }
    cout << endl;
    int sizetester = tester.size();
    for (int j = 2; j <= 6; j++){
        cout << "j is " << j << ' ';
        for (int k = 0; k < sizetester; k++){
            if (j*x == tester[k]){
                cout << "counter increased because x, counter " << x << " " << counter << endl;
                counter++;
                if (counter == 6){
                    cout << "Number is " << x << endl;
                    return true;
                }
                break;
            }
        }
        //cout << "Number " << x << " failed" << endl;
        return false;
    }
    return true;
}

int main(){
    unsigned long long x = 1;
    for (double i = 0; ; i++){
        cout << i << endl;
        while (x < 1.67*pow(10, i)){
            if (i == 5)
                cout << x << endl;
            if (checkPerm(x)){
                cin.get();
            }
            x++;
        }
        x = pow(10, (i + 1));
    }
    cin.get();
}

在这段代码中存在以下问题:

cout << "x is " << strx << " and its permutations are ";
while (next_permutation(strx.begin(), strx.end())){
    cout << strx << " ";
    unsigned long long stoipermstrx = stoi(strx);
    tester.push_back(stoipermstrx);
}
cout << endl;
int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
    cout << "j is " << j << ' ';
    for (int k = 0; k < sizetester; k++){
        if (j*x == tester[k]){
            cout << "counter increased because x, counter " << x << " " << counter << endl;
            counter++;
            if (counter == 6){
                cout << "Number is " << x << endl;
                return true;
            }
            break;
        }
    }
    //cout << "Number " << x << " failed" << endl;
    return false;
}

这里输出将是“j是j x是x并且其排列是(x的排列)”。但是,控制台应该打印“x是x,它的排列是(排列)j是j”。给出以下示例输出:

j is 2 x is 1355 and its permutations are 1535 1553 3155 3515 3551 5135 5153 531
5 5351 5513 5531
j is 2 x is 1356 and its permutations are 1365 1536 1563 1635 1653 3156 3165 351
6 3561 3615 3651 5136 5163 5316 5361 5613

1 个答案:

答案 0 :(得分:2)

这似乎有两个(次要的)事情。一,在打印sizetester的值之前,您没有查看j的值,并且您没有在j的值之后打印换行符。这意味着您在当前&#39; <&#39; 的行的开头显示上一个循环j值。如果我理解你的代码应该做什么,它似乎正确地做到了 - 它只是输出显示的方式使它混乱。

试试这个:

int sizetester = tester.size();
for (int j = 2; j <= 6; j++){
    if (sizetester){                           // <-- added test (see below)
        cout << "j is " << j << '\n';          // <-- added newline
    }                                          // <--

针对sizetester的测试可以抑制j的虚假打印值 - 您稍后会测试(k < sizetester)。换行符只是阻止j的值开始下一个x 值的行,这似乎是导致混淆输出的原因。