-5

重複の可能性:
特定の要素の順列を出力するプログラム

だから、私は番号153を持っており、私のプログラムは135、153、315、351、513、531を出力する必要があります。つまり、すべての可能な数字です。それを行う方法はありますか?

4

3 に答える 3

0
#include<iostream>
#include<vector>

void swap(int& a, int& b)
{ int x=a;
a=b;
b=x;
}

void printVector(std::vector<int>& theVector)
{ for (unsigned int i=0; i<theVector.size(); i++)
std::cout << theVector[i] << ",";
std::cout << "\n";
}

void generateAllPermutations(std::vector<int>& toBePermuted, unsigned int nextIndex)
{ if (nextIndex==toBePermuted.size())
{ printVector(toBePermuted);
return;
}
for (unsigned int i=nextIndex; i<toBePermuted.size(); i++)
{ swap(toBePermuted[i], toBePermuted[nextIndex]);
generateAllPermutations(toBePermuted, nextIndex+1);
swap(toBePermuted[i], toBePermuted[nextIndex]);
}
}

void generateAllPermutations(std::vector<int>& toBePermuted)
{ generateAllPermutations(toBePermuted, 0);
}

int main()
{ std::vector<int> theVector;

theVector.push_back(1);
theVector.push_back(3);
theVector.push_back(5);
generateAllPermutations(theVector);
//note: you will have to print out the vector yourself
}
于 2012-12-11T17:53:36.327 に答える
0

これはフットプリントの小さな部分です, それはあなたを助けるかもしれません

#include <stdio.h>

int main(int argc, char** argv){
  int a,b,c;

  for(a=1; a<4; a++){
    for(b=1; b<4; b++){
      for(c=1; c<4; c++){
      if(!(a==b || a==c || b==c))
        printf("%d%d%d\n",a,b,c);
      }
    }
  }

  return 0;
}
于 2012-12-11T17:57:44.740 に答える
0

int から string に変更してから、char の配列に変更します。次に、ネストされたループを使用します。

于 2012-12-11T17:42:18.923 に答える