您的位置:首页 > 其它

翻转01字符串中的?,并列出所有的可能输出

2014-08-25 11:47 267 查看
"1100?1?001?1“

?可以为’0‘或者’1‘,把字符串中所有的可能列出

// zero_one_and_query.cpp : Defines the entry point for the console application.
//

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

using namespace std;

void printString(char *Array, int n)
{
for (int i=0; i<n; i++)
{
printf("%c ", Array[i]);
}
printf("\n");
}

void printAllInternal(char *Array, int n, int nextIdx, std::vector<int> &posVector)
{
if (posVector[nextIdx] == -1)
{
printString(Array, n);
return;
}

for (int idx=0; idx<2; idx++)
{
Array[posVector[nextIdx]] = idx-0+'0';
printAllInternal(Array, n, nextIdx+1, posVector);
}
}

void printAll(char *Array, int n)
{
if (Array == NULL || n == 0)
return;

std::vector<int> posVector;
bool foundFlag = false;

for (int i=0; i<n; i++)
{
if (Array[i] == '?')
{
posVector.push_back(i);
foundFlag = true;
}
}

if (foundFlag)
{
posVector.push_back(-1);
}
else
{
printString(Array, n);
return;
}

for (int idx=0; idx<2; idx++)
{
Array[posVector[0]] = (idx-0)+'0';
printAllInternal(Array, n, 1, posVector);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
char Array[] = {"1100?1?001?1"};

printAll(Array, strlen(Array));

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐