翻转01字符串中的?,并列出所有的可能输出
2014-08-25 11:47
267 查看
"1100?1?001?1“
?可以为’0‘或者’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; }
相关文章推荐
- 字符串的所有可能组合输出
- 网易有道9-13北邮笔试题:输入字符串,将字符串里的*用0或1替换,输出所有可能的字符串
- 输入一串不重复的字符串,列出所有可能的组合(java实现)
- 给定一个字符串,输出其所有的可能排列
- 给定一个字符串,字符串中的*可以替换成0或1,输出所有可能的结果
- 题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。
- 给定一个字符串,里面用空格分开为多个(>=6)部分组合,如:01 02 03 04 05 06 07 …… 写一个函数返回任意6个组合的字符串: 输出格式实例: 01 02 03 04 05 06 0
- 24点游戏的c++代码,能够输出所有可能的解
- 输入一个正整数,若该数能用几个连续正整数之和表示,则输出所有可能的正整数序列
- BOM查找指定子项的所有父项,并以字符串的形式输出
- 输出一个字符串所有排列。注意有重复字符
- 题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。
- 将字符串实现翻转输出,例如将“abc”转换为“cba”
- 滚雪球思路 计算一个字符串所有可能的排列顺序
- Page_Load中指给客户端输出一个字符串 而不是所有的html内容也输出 出去 ;输出之后会发现在客户端接收的时候 responseText有值 但是responseXML没值
- 写一个函数,输出字符串中字符的所有排序。(比如:abc acb bac bca cab cba )
- 输入一个字符串,输出该字符串中字符的所有组合 C#【总结】
- 列出字符串中个数最多所有的字符
- 输出字符串中出现过的所有字符,相同字符只输出一次
- IT公司机试常考的一道题--将任意字符串逆序输出或者输出所有排列