9610
BAN USERmy working code. please correct me if something is wrong.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void phoneNum(string set, string src, int numLeft) {
if (numLeft == 0) {
cout << src << endl;
return;
}
for (int i = 0; i < set.length(); ++i){
if (src.length() == 0) {
phoneNum(set, src + set[i], numLeft - 1);
} else {
if (set[i] != src[src.length() - 1]) {
phoneNum(set, src + set[i], numLeft - 1);
}
}
}
}
void phone(int n, vector<char> disAllowed) {
string tmp = "0123456789";
for (int i = 0; i < tmp.length(); ++i){
for (int j = 0; j < disAllowed.size(); ++j) {
if (tmp[i] == disAllowed[j]) {
tmp = tmp.substr(0, i) + tmp.substr(i + 1, tmp.length() - i - 1);
}
}
}
bool hasFour = false;
string tmpNoFour = "";
for (int i = 0; i < tmp.length(); ++i) {
if(tmp[i] == '4') {
hasFour = true;
tmpNoFour = tmp.substr(0, i) + tmp.substr(i + 1, tmp.length() - i - 1);
}
}
if (hasFour) {
phoneNum(tmp, "4", n - 1);
phoneNum(tmpNoFour, "", n);
} else {
phoneNum(tmp, "", n);
}
}
void main(){
char a[] = {'1','2','3'};
vector<char> b(&a[0], &a[0] + 3);
phone(3, b);
cin.get();
}
my working code
#include <iostream>
#include <string>
using namespace std;
void phoneNum(string set, string src, int numLeft) {
if (numLeft == 0) {
cout << src ;
return;
}
for (int i = 0; i < set.length(); ++i){
if (src.length() == 0) {
phoneNum(set, src + set[i], numLeft - 1);
} else {
if (set[i] != src[src.length() - 1]) {
phoneNum(set, src + set[i], numLeft - 1);
}
}
}
}
void phone(int n) {
phoneNum("013568", "4", n - 1);
phoneNum("013568", "", n);
}
void main(){
phone(5);
cin.get();
}
#include <iostream>
#include <string>
using namespace std;
void printPermutation(string src, string set, int numLeft) {
if (numLeft == 0) {
cout << src << endl;
return;
}
for (int i = 0; i < set.length(); ++i) {
printPermutation(src + set[i], set.substr(0, i) + set.substr(i + 1, set.length() - i - 1), numLeft - 1);
}
}
void main() {
printPermutation("", "0123456789", 2);
cin.get();
}
working
- 9610 October 19, 2014