Exemple #1
1
	void genPerms(deque<int> & unvisited, vector<int> & path) {
		if(unvisited.empty()) {
			ret.push_back(path);
		}

		for(unsigned k = 0; k < unvisited.size(); k++) {
			path.push_back(unvisited.front());
			unvisited.pop_front();
			genPerms(unvisited, path);
			unvisited.push_back(path.back());
			path.pop_back();
		}
	}
Exemple #2
0
int main(){
    int i, j, k, m;
    int permutations[4*3*2] = {0};
    int currDiff;

    genPrimes();

    for (i = 1001; i < 10000; i += 2){
        if (primes[i]){
            permutations[0] = i;
            genPerms(permutations); /*only prime perms*/
            for (j = 0; permutations[j+2] && j < (4*3*2); j++){
                for (k = j+1; permutations[k+1] && k < (4*3*2); k++){
                    currDiff = permutations[k] - permutations[j];
                    for (m = k+1; permutations[m] && m < (4*3*2); m++){
                        if (permutations[m] == permutations[k] + currDiff){
                            printf("%d%d%d\n", permutations[j], permutations[k],
                                    permutations[m]);
                            return 0;
                        }
                    }
                }
            }

            for (j = 0; permutations[j] && j < 4*3*2; j++){
                primes[j] = 0;
            }    
        }

        memset(permutations, 0, 4*3*2*sizeof(int));
    }

    return -1;
}
Exemple #3
0
 vector<vector<int>> permute(vector<int>& nums) {
 	deque<int> unvisited(nums.begin(), nums.end());
 	vector<int> path;
 	genPerms(unvisited, path);
     return ret;
 }