fork download
  1. #include <iostream> // cin, cout
  2. #include <iomanip> // setprecision, fixed
  3. #include <cstdlib> // rand, srand
  4. #include <ctime> // time
  5. using namespace std;
  6.  
  7. int dem_trung(const int a[], const int b[]);
  8. void sinh_so_ngau_nhien(int ve[]);
  9. long long tien_thuong(int x);
  10. void mo_phong(int N, const int kq[]);
  11.  
  12. int main() {
  13. srand(time(0));
  14. int N;
  15. cout << "Nhap so luong ve can mo phong: ";
  16. cin >> N;
  17. int kq[6];
  18. cout << "Nhap 6 so trung thuong (1..45):\n";
  19. for (int i = 0; i < 6; i++) {
  20. cout << "So thu " << i + 1 << ": ";
  21. cin >> kq[i];
  22. }
  23. mo_phong(N, kq);
  24. return 0;
  25. }
  26.  
  27. // Đếm số trùng giữa hai vé
  28. int dem_trung(const int a[], const int b[]) {
  29. int cnt = 0;
  30. for (int i = 0; i < 6; i++) {
  31. for (int j = 0; j < 6; j++) {
  32. if (a[i] == b[j]) cnt++;
  33. }
  34. }
  35. return cnt;
  36. }
  37.  
  38. // Sinh 6 số ngẫu nhiên từ 1..45, không trùng
  39. void sinh_so_ngau_nhien(int ve[]) {
  40. int used[46] = {0};
  41. int dem = 0;
  42. while (dem < 6) {
  43. int x = rand() % 45 + 1;
  44. if (!used[x]) {
  45. used[x] = 1;
  46. ve[dem++] = x;
  47. }
  48. }
  49. }
  50.  
  51. // Trả về tiền thưởng theo số trùng
  52. long long tien_thuong(int x) {
  53. if (x == 3) return 30000;
  54. if (x == 4) return 300000;
  55. if (x == 5) return 10000000;
  56. if (x == 6) return 120000000;
  57. return 0;
  58. }
  59.  
  60. // Mô phỏng N vé, in ra từng vé và tính tỉ lệ hoàn vốn
  61. void mo_phong(int N, const int kq[]) {
  62. long long gia_ve = 10000;
  63. long long tong_tien = gia_ve * N;
  64. long long tien_thu = 0;
  65. cout << "Dang mo phong " << N << " ve...\n";
  66. for (int i = 0; i < N; i++) {
  67. int ve[6];
  68. sinh_so_ngau_nhien(ve);
  69. int so_trung = dem_trung(ve, kq);
  70. long long tienThuong = tien_thuong(so_trung);
  71. tien_thu += tienThuong;
  72. // In ra vé và kết quả
  73. cout << "Ve " << i + 1 << ": ";
  74. for (int j = 0; j < 6; j++) cout << ve[j] << " ";
  75. cout << "| So trung: " << so_trung;
  76. cout << " | Tien thuong: " << tienThuong << "\n";
  77. }
  78. double ty_le = (double)tien_thu / tong_tien * 100;
  79. cout << fixed << setprecision(6) << "Ty le hoan von: " << ty_le << "%\n";
  80. }
  81.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Nhap so luong ve can mo phong: Nhap 6 so trung thuong (1..45):
So thu 1: So thu 2: So thu 3: So thu 4: So thu 5: So thu 6: Dang mo phong 0 ve...
Ty le hoan von: -nan%