fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. void cardGenerator(int rows, int cols)
  6. {
  7. vector<int> colPool [cols];
  8.  
  9.  
  10. for(int j=0;j<cols;j++)
  11. {
  12. int start = j*10 +1;
  13. int end = (j+1)*10;
  14. for(int k=start;k<=end;k++)
  15. {
  16. colPool[j].push_back(k);
  17. }
  18. shuffle(colPool[j].begin(),colPool[j].end(),mt19937(random_device{}()));
  19.  
  20. }
  21.  
  22. vector<vector<int>> card(rows,vector<int>(cols,0));
  23. vector <int> rowCount (rows,0);
  24. for(int j=0;j<cols;j++)
  25. {
  26. int row = j%rows;
  27. card[row][j] = colPool[j].back();
  28. colPool[j].pop_back();
  29. rowCount[row]++;
  30. }
  31. //pick random col
  32. vector<int> randomColNo (cols,0);
  33. for(int i=0;i<cols;i++)
  34. {
  35. randomColNo[i]=i;
  36. }
  37. shuffle(randomColNo.begin(),randomColNo.end(),mt19937(random_device{}()));
  38. for(int i=0;i<rows;i++)
  39. {
  40. for(int j =0;j<cols;j++)
  41. {
  42. if(card[i][randomColNo[j]]==0)
  43. {
  44. card[i][randomColNo[j]] = colPool[randomColNo[j]].back();
  45. colPool[randomColNo[j]].pop_back();
  46. rowCount[i]++;
  47. if(rowCount[i]==5)
  48. {
  49. break;
  50. }
  51. }
  52.  
  53. }
  54. }
  55. for(int j=0;j<cols;j++)
  56. {
  57. vector <int> temp;
  58. for(int i=0;i<rows;i++)
  59. {
  60. if(card[i][j]!=0)
  61. {
  62. temp.push_back(card[i][j]);
  63. }
  64. }
  65. sort(temp.begin(),temp.end());
  66. int k=0;
  67. for(int i=0;i<rows;i++)
  68. {
  69. if(card[i][j]!=0)
  70. {
  71. card[i][j] = temp[k];
  72. k++;
  73. }
  74. }
  75.  
  76. }
  77. for(int i=0;i<rows;i++)
  78. {
  79. cout<<"\n";
  80. for(int j=0;j<cols;j++)
  81. {
  82. cout<<card[i][j]<<" ";
  83. }
  84. }
  85.  
  86.  
  87. }
  88.  
  89. int main() {
  90. // your code goes here
  91. cardGenerator (3,9);
  92. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
1 0 0 33 0 55 70 0 86 
5 17 0 0 45 0 0 75 87 
8 0 27 34 0 58 0 0 88