rotti321

permutari

Oct 20th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define dim 100
  3. using namespace std;
  4.  
  5. ifstream f("permutari.in");
  6. ofstream g("permutari.out");
  7.  
  8. int sol[dim],n,nr=0;
  9.  
  10. int solutie(int k)
  11. {
  12. return(k-1==n);
  13.  
  14. }
  15. void init(int k)
  16. {
  17. sol[k]=0;
  18. }
  19.  
  20. void tipar()
  21. {
  22. for(int i=1;i<=n;i++){
  23. g<<sol[i]<<" ";
  24. }
  25. g<<endl;
  26. }
  27.  
  28. int valid(int k)
  29. {
  30. for(int i=1;i<=k-1;i++){
  31. if(sol[i]==sol[k])
  32. return 0;
  33. }
  34. return 1;
  35. }
  36.  
  37. int succesor(int k)
  38. {
  39. if(sol[k]<n){
  40. sol[k]++;
  41. return 1;
  42. }
  43. return 0;
  44. }
  45.  
  46. void citire()
  47. {
  48. f>>n;
  49. }
  50.  
  51. void backtr(int k)
  52. {
  53. if (solutie(k))
  54. tipar();
  55. else{
  56. init(k);
  57. while(succesor(k)){
  58. if(valid(k)){
  59. backtr(k+1);
  60. }
  61. }
  62. }
  63. }
  64. int main()
  65. {
  66. citire(); backtr(1);
  67. return 0;
  68. }
Add Comment
Please, Sign In to add comment