1

我正在尝试将适用于“0”-“3”字符串的代码转换为整数,以便它适用于更高的数字

#include <string>
#include <iostream>
using namespace std;

void permutate(char[], int );
bool recurse(char[], int );

int main()
{
    int strLength;
    cout << "Enter your desired length: ";
    cin >> strLength;
    char strArray[strLength];

    for (int i = 0; i<strLength; i++)
        strArray[i] = '0';

    permutate(strArray, sizeof(strArray));

    return 0;
}

void permutate(char charArray[], int length)
{
    string wait;
    length--;
    bool done = false;

    while(!done)
    {
        for (int i = 0; i <= length; i++)
            cout << charArray[i];
        cout << endl;

        if (charArray[length] == '3')
            done = recurse(charArray, length);
        else
            charArray[length] = (char)(charArray[length]+1);

    }
}

bool recurse(char charArray[], int length)
{
    bool done = false;
    int temp = length;
    if (temp > 1)
    {
        charArray[temp] = '0';
        if (charArray[temp-1] == '3')
        {
            temp--;
            done = recurse(charArray, temp);
        }
        else
            (charArray[temp-1] = (char)(charArray[temp-1] + 1));

    }
    else
    {
        charArray[temp] = '0';
        if (charArray[temp-1] == '3')
            done = true;
        else
            charArray[temp-1] = (char)(charArray[temp-1]+1);
    }
    return done;
}

我把每个字符都改成了int,

  • 每 '0' = 0, '3' = 3
  • 每 (charArray[temp-1] = (char)(charArray[temp-1] + 1)); 到 charArray[temp-1]++;

我试图调试,但我仍然无法使它工作:(

设法修复它(适用于大量数字):

#include <string>
#include <iostream>
using namespace std;

void permutate(int[], int, int );
bool recurse(int[], int, int );

int main()
{
int strLength, nrElem;
cout << "Enter your desired length: ";
cin >> strLength;
cout << "Enter nr elem: ";
cin >> nrElem;
int strArray[strLength];

for (int i = 0; i<strLength; i++)
strArray[i] = 0;

permutate(strArray, strLength, nrElem );
cout << "\nSTOP";
return 0;
}

void permutate(int charArray[], int length, int nrElem)
{

//  length--;
    bool done = false;

    while(!done)
    {

    for (int i = 0; i < length; i++)
    cout << charArray[i] << " ";

    cout << endl;


    if (charArray[length - 1] == nrElem)
        //done = true;
        done = recurse(charArray, length, nrElem);
    else
        charArray[length - 1]++;

}
}

bool recurse(int charArray[], int length, int nrElem)
{
bool done = false;
int temp = length ; 
if (temp > 1)
{
charArray[temp] = 0;
if (charArray[temp-1] == nrElem)
{
temp--;
done = recurse(charArray, temp, nrElem);
}
else
charArray[temp-1]++;

}
else
{
charArray[temp] = 0;
if (charArray[temp-1] == nrElem)
done = true;
else
charArray[temp-1]++;
}
return done;
}
4

3 回答 3

0

在您的permutate函数中,您正在递增charArray[length]但检查是否charArray[length - 1]等于nrElem,因此您永远不会最终调用recurse.

于 2013-10-29T14:40:20.287 回答
0

嗯......为什么不简单地制作一个排列算法,然后使用一个通用函数来打印你正在排列的任何东西。这是我对字符串的处理方式:

#include <iostream>
#include <string>

template<class T> 
void print(T * A, unsigned n){ //for printing purposes
    for(unsigned i=0;i<n;i++){
    std::cout<<A[i]<<" ";
    }
    std::cout<<std::endl;
}

void generate_permutations(unsigned k, std::string str, char *A, bool *U){
// k is the position that we need to fill, starts from 0 and goes to the end.
if(k<str.size()) //if k==str.size() then we will print it
    for(unsigned i=0;i<str.size();i++){
        if(U[i]==0){
            A[k]=str[i]; U[i]=1;
            generate_permutations(k+1, str, A,U);
            U[i]=0; //after the recursion is finished and printed, we can release the letter.
        }   
    }
else
    print(A,str.size());
}

int main(){
std::string str;
std::cout<<"Enter the string to be permutated: \n";
std::cin>>str;

int n;
n = str.length(); // You don't really need to ask the user the size of the string he/she wants to enter.

bool *U; // we will keep track of the used letters with the help of this boolean vector
char *A; // we will copy the contents of str here, so that we keep the str intact

    U = new bool[n];    
    for (int i=0;i<n;i++) U[i]=false;

    A = new char[n];
    for (int i=0;i<n;i++) A[i]=str[i];

generate_permutations(0,str,A,U);
return 0;
}

现在如果你想转换为数字(整数),它几乎是一样的:

#include <iostream>

template<class T>
void print(T * A, int n){
for(int i=0;i<n;i++){
    std::cout<<A[i]<<" ";
}
std::cout<<std::endl;
}

void generate_permutations(int k, int *A, bool *U, int n){
if(k==n) 
    print(A,n);
else {
    for(int i=0;i<n;i++){
        if(U[i]==0){
            A[k]=i; U[i]=1;
            generate_permutations(k+1,A,U,n);
            U[i]=0;
        }
    }
}
}

int main(){
int n;
std::cout<<"Permutations of how many objects? \n";
std::cin>>n;
int * A;
bool *U;
A = new int[n];
U = new bool[n];    
for (int i=0;i<n;i++) U[i]=false;
print(U, n);
generate_permutations(0,A,U,n);
return 0;
}
于 2015-04-26T11:36:07.067 回答
0

这是执行相同操作的一小段代码(不是答案,但在注释字段中看起来不正确),不确定您是否需要递归,如果您不需要此代码可能会感兴趣:

#include <iostream>
#include <sstream>
using namespace std;

string output(int firstIntSize, int secondIntSize)
{
    std::ostringstream oss;
    for (int i = 0; i<firstIntSize; i++)
    {
        for (int j = 0; j< secondIntSize; j++)
        {  
            oss << i << j << " "; 
        }
    }
    return oss.str();
}
int main()
{
    cout << output(2,3);
    return 0;
}
于 2013-10-29T15:34:31.153 回答