fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int get_unit_digit_sum(int a, int b) {
  6. return (a + b) % 10;
  7. }
  8.  
  9. int main() {
  10. int initial_p_money, initial_h_money;
  11. int bet_per_round;
  12. int round;
  13. int p_money, h_money;
  14. int p_win_count, h_win_count;
  15. int p_streak, h_streak;
  16. int p_max_streak, h_max_streak;
  17. int day = 1;
  18. int total_round = 0;
  19. int total_p_win_count = 0, total_h_win_count = 0;
  20. int total_p_streak = 0, total_h_streak = 0;
  21. int target_total_money;
  22.  
  23. srand(time(NULL));
  24.  
  25. printf("กรอกจำนวนเงินเริ่มต้นของผู้เล่น: ");
  26. scanf("%d", &initial_p_money);
  27. printf("กรอกจำนวนเงินเริ่มต้นของเจ้ามือ: ");
  28. scanf("%d", &initial_h_money);
  29. printf("กรอกจำนวนเงินเดิมพันต่อรอบ: ");
  30. scanf("%d", &bet_per_round);
  31. printf("กรอกจำนวนเงินเป้าหมายรวมของ P (เงินคงเหลือถึงจำนวนนี้จะตัดจบ): ");
  32. scanf("%d", &target_total_money);
  33.  
  34. char temp_filename[] = "/storage/emulated/0/Download/CodingC/log_tmp.txt";
  35. FILE *fp = fopen(temp_filename, "w");
  36. if (fp == NULL) {
  37. printf("ไม่สามารถสร้างไฟล์บันทึกได้\n");
  38. return 1;
  39. }
  40.  
  41. while (1) {
  42. p_money = initial_p_money;
  43. h_money = initial_h_money;
  44. p_win_count = h_win_count = 0;
  45. p_streak = h_streak = p_max_streak = h_max_streak = 0;
  46. round = 1;
  47.  
  48. fprintf(fp, "=== วันที่ %d ===\n", day);
  49. printf("\n=== วันที่ %d ===\n", day);
  50.  
  51. int prev_p_money = p_money;
  52. int prev_h_money = h_money;
  53.  
  54. while (p_money >= bet_per_round && h_money >= bet_per_round) {
  55. prev_p_money = p_money;
  56. prev_h_money = h_money;
  57.  
  58. int p_number = rand() % 37;
  59. int h_number = rand() % 37;
  60.  
  61. p_money -= bet_per_round;
  62. h_money -= bet_per_round;
  63.  
  64. int p_point = get_unit_digit_sum(p_number / 10, p_number % 10);
  65. int h_point = get_unit_digit_sum(h_number / 10, h_number % 10);
  66.  
  67. printf("รอบที่ %d\n", round);
  68. fprintf(fp, "รอบที่ %d\n", round);
  69. printf("P ได้ %d (%d)\n", p_number, p_point);
  70. fprintf(fp, "P ได้ %d (%d)\n", p_number, p_point);
  71. printf("H ได้ %d (%d)", h_number, h_point);
  72. fprintf(fp, "H ได้ %d (%d)", h_number, h_point);
  73.  
  74. if (p_point == 0 && h_point != 0) {
  75. // P ชนะด้วย 0 (จ่าย 3x)
  76. // P ได้เงินเดิมพันของตัวเอง + เดิมพันของ H + ได้เงินเพิ่มอีก 1 เท่า
  77. p_money += bet_per_round * 3;
  78. // H ถูกหักเพิ่มอีก 1 เท่า (นอกจากที่หักไปตอนแทงแล้ว)
  79. h_money -= bet_per_round;
  80. p_win_count++; p_streak++;
  81. h_streak = 0;
  82. if (p_streak > p_max_streak) p_max_streak = p_streak;
  83. printf(" ===== P ชนะด้วยแต้ม 0! จ่าย 3x \n");
  84. fprintf(fp, " ===== P ชนะด้วยแต้ม 0! จ่าย 3x \n");
  85. } else if (h_point == 0 && p_point != 0) {
  86. // H ชนะด้วย 0
  87. h_money += bet_per_round * 2;
  88. h_win_count++; h_streak++;
  89. p_streak = 0;
  90. if (h_streak > h_max_streak) h_max_streak = h_streak;
  91. printf(" ===== H ชนะด้วยแต้ม 0! ได้เงินเดิมพัน x2\n");
  92. fprintf(fp, " ===== H ชนะด้วยแต้ม 0! ได้เงินเดิมพัน x2\n");
  93. } else if (p_point > h_point) {
  94. // P ชนะปกติ
  95. p_money += bet_per_round * 2;
  96. p_win_count++; p_streak++;
  97. h_streak = 0;
  98. if (p_streak > p_max_streak) p_max_streak = p_streak;
  99. printf(" ===== P ชนะ\n");
  100. fprintf(fp, " ===== P ชนะ\n");
  101. } else {
  102. // H ชนะปกติ
  103. h_money += bet_per_round * 2;
  104. h_win_count++; h_streak++;
  105. p_streak = 0;
  106. if (h_streak > h_max_streak) h_max_streak = h_streak;
  107. printf(" ===== H ชนะ\n");
  108. fprintf(fp, " ===== H ชนะ\n");
  109. }
  110.  
  111. int p_diff = p_money - prev_p_money;
  112. int h_diff = h_money - prev_h_money;
  113.  
  114. printf("เงินคงเหลือ: P = %d (%+d) | H = %d (%+d)\n\n", p_money, p_diff, h_money, h_diff);
  115. fprintf(fp, "เงินคงเหลือ: P = %d (%+d) | H = %d (%+d)\n\n", p_money, p_diff, h_money, h_diff);
  116.  
  117. round++;
  118. total_round++;
  119.  
  120. if (p_money >= target_total_money) {
  121. break;
  122. }
  123. }
  124.  
  125. double p_win_rate_day = (double)p_win_count * 100 / (p_win_count + h_win_count);
  126. double h_win_rate_day = (double)h_win_count * 100 / (p_win_count + h_win_count);
  127.  
  128. total_p_win_count += p_win_count;
  129. total_h_win_count += h_win_count;
  130.  
  131. if (p_streak > total_p_streak) total_p_streak = p_streak;
  132. if (h_streak > total_h_streak) total_h_streak = h_streak;
  133.  
  134. fprintf(fp, "--- จบวันที่ %d ---\n", day);
  135. fprintf(fp, "P ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", p_win_count, p_max_streak);
  136. fprintf(fp, "H ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", h_win_count, h_max_streak);
  137. fprintf(fp, "เปอร์เซ็นต์ชนะประจำวันของ P: %.2f%%\n", p_win_rate_day);
  138. fprintf(fp, "เปอร์เซ็นต์ชนะประจำวันของ H: %.2f%%\n\n", h_win_rate_day);
  139.  
  140. printf("--- จบวันที่ %d ---\n", day);
  141. printf("P ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", p_win_count, p_max_streak);
  142. printf("H ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", h_win_count, h_max_streak);
  143. printf("เปอร์เซ็นต์ชนะประจำวันของ P: %.2f%%\n", p_win_rate_day);
  144. printf("เปอร์เซ็นต์ชนะประจำวันของ H: %.2f%%\n\n", h_win_rate_day);
  145.  
  146. if (p_money >= target_total_money || (h_money <= 0 && p_money > 0)) {
  147. if (p_money >= target_total_money) {
  148. fprintf(fp, "### ผู้เล่นชนะ! เพราะทำกำไรถึงเป้าหมาย %d บาท ในวันที่ %d ###\n\n", target_total_money, day);
  149. printf("### ผู้เล่นชนะ! เพราะทำกำไรถึงเป้าหมาย %d บาท ในวันที่ %d ###\n\n", target_total_money, day);
  150. } else {
  151. fprintf(fp, "### ผู้เล่นชนะ! เจ้ามือหมดตัวในวันที่ %d ###\n\n", day);
  152. printf("### ผู้เล่นชนะ! เจ้ามือหมดตัวในวันที่ %d ###\n\n", day);
  153. }
  154.  
  155. double p_win_rate_total = (double)total_p_win_count * 100 / total_round;
  156. double h_win_rate_total = (double)total_h_win_count * 100 / total_round;
  157.  
  158. fprintf(fp, "=== สรุปรวมตั้งแต่วันแรกจนวันที่ %d ===\n", day);
  159. fprintf(fp, "รอบที่เล่นทั้งหมด: %d\n", total_round);
  160. fprintf(fp, "P ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_p_win_count, total_p_streak);
  161. fprintf(fp, "H ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_h_win_count, total_h_streak);
  162. fprintf(fp, "เปอร์เซ็นต์ชนะรวมของ P: %.2f%%\n", p_win_rate_total);
  163. fprintf(fp, "เปอร์เซ็นต์ชนะรวมของ H: %.2f%%\n\n", h_win_rate_total);
  164.  
  165. printf("=== สรุปรวมตั้งแต่วันแรกจนวันที่ %d ===\n", day);
  166. printf("รอบที่เล่นทั้งหมด: %d\n", total_round);
  167. printf("P ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_p_win_count, total_p_streak);
  168. printf("H ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_h_win_count, total_h_streak);
  169. printf("เปอร์เซ็นต์ชนะรวมของ P: %.2f%%\n", p_win_rate_total);
  170. printf("เปอร์เซ็นต์ชนะรวมของ H: %.2f%%\n\n", h_win_rate_total);
  171.  
  172. fclose(fp);
  173.  
  174. char final_filename[150];
  175. snprintf(final_filename, sizeof(final_filename),
  176. "/storage/emulated/0/Download/CodingC/%d_ชนะวันที่ %d.txt",
  177. initial_p_money + initial_h_money, day);
  178. rename(temp_filename, final_filename);
  179.  
  180. break;
  181. }
  182.  
  183. day++;
  184. }
  185.  
  186. return 0;
  187. }
Success #stdin #stdout 0.04s 25688KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int get_unit_digit_sum(int a, int b) {
    return (a + b) % 10;
}

int main() {
    int initial_p_money, initial_h_money;
    int bet_per_round;
    int round;
    int p_money, h_money;
    int p_win_count, h_win_count;
    int p_streak, h_streak;
    int p_max_streak, h_max_streak;
    int day = 1;
    int total_round = 0;
    int total_p_win_count = 0, total_h_win_count = 0;
    int total_p_streak = 0, total_h_streak = 0;
    int target_total_money;

    srand(time(NULL));

    printf("กรอกจำนวนเงินเริ่มต้นของผู้เล่น: ");
    scanf("%d", &initial_p_money);
    printf("กรอกจำนวนเงินเริ่มต้นของเจ้ามือ: ");
    scanf("%d", &initial_h_money);
    printf("กรอกจำนวนเงินเดิมพันต่อรอบ: ");
    scanf("%d", &bet_per_round);
    printf("กรอกจำนวนเงินเป้าหมายรวมของ P (เงินคงเหลือถึงจำนวนนี้จะตัดจบ): ");
    scanf("%d", &target_total_money);

    char temp_filename[] = "/storage/emulated/0/Download/CodingC/log_tmp.txt";
    FILE *fp = fopen(temp_filename, "w");
    if (fp == NULL) {
        printf("ไม่สามารถสร้างไฟล์บันทึกได้\n");
        return 1;
    }

    while (1) {
        p_money = initial_p_money;
        h_money = initial_h_money;
        p_win_count = h_win_count = 0;
        p_streak = h_streak = p_max_streak = h_max_streak = 0;
        round = 1;

        fprintf(fp, "=== วันที่ %d ===\n", day);
        printf("\n=== วันที่ %d ===\n", day);

        int prev_p_money = p_money;
        int prev_h_money = h_money;

        while (p_money >= bet_per_round && h_money >= bet_per_round) {
            prev_p_money = p_money;
            prev_h_money = h_money;

            int p_number = rand() % 37;
            int h_number = rand() % 37;

            p_money -= bet_per_round;
            h_money -= bet_per_round;

            int p_point = get_unit_digit_sum(p_number / 10, p_number % 10);
            int h_point = get_unit_digit_sum(h_number / 10, h_number % 10);

            printf("รอบที่ %d\n", round);
            fprintf(fp, "รอบที่ %d\n", round);
            printf("P ได้ %d (%d)\n", p_number, p_point);
            fprintf(fp, "P ได้ %d (%d)\n", p_number, p_point);
            printf("H ได้ %d (%d)", h_number, h_point);
            fprintf(fp, "H ได้ %d (%d)", h_number, h_point);

            if (p_point == 0 && h_point != 0) {
                // P ชนะด้วย 0 (จ่าย 3x)
                // P ได้เงินเดิมพันของตัวเอง + เดิมพันของ H + ได้เงินเพิ่มอีก 1 เท่า
                p_money += bet_per_round * 3;
                // H ถูกหักเพิ่มอีก 1 เท่า (นอกจากที่หักไปตอนแทงแล้ว)
                h_money -= bet_per_round;
                p_win_count++; p_streak++;
                h_streak = 0;
                if (p_streak > p_max_streak) p_max_streak = p_streak;
                printf(" ===== P ชนะด้วยแต้ม 0! จ่าย 3x \n");
                fprintf(fp, " ===== P ชนะด้วยแต้ม 0! จ่าย 3x \n");
            } else if (h_point == 0 && p_point != 0) {
                // H ชนะด้วย 0
                h_money += bet_per_round * 2;
                h_win_count++; h_streak++;
                p_streak = 0;
                if (h_streak > h_max_streak) h_max_streak = h_streak;
                printf(" ===== H ชนะด้วยแต้ม 0! ได้เงินเดิมพัน x2\n");
                fprintf(fp, " ===== H ชนะด้วยแต้ม 0! ได้เงินเดิมพัน x2\n");
            } else if (p_point > h_point) {
                // P ชนะปกติ
                p_money += bet_per_round * 2;
                p_win_count++; p_streak++;
                h_streak = 0;
                if (p_streak > p_max_streak) p_max_streak = p_streak;
                printf(" ===== P ชนะ\n");
                fprintf(fp, " ===== P ชนะ\n");
            } else {
                // H ชนะปกติ
                h_money += bet_per_round * 2;
                h_win_count++; h_streak++;
                p_streak = 0;
                if (h_streak > h_max_streak) h_max_streak = h_streak;
                printf(" ===== H ชนะ\n");
                fprintf(fp, " ===== H ชนะ\n");
            }

            int p_diff = p_money - prev_p_money;
            int h_diff = h_money - prev_h_money;

            printf("เงินคงเหลือ: P = %d (%+d) | H = %d (%+d)\n\n", p_money, p_diff, h_money, h_diff);
            fprintf(fp, "เงินคงเหลือ: P = %d (%+d) | H = %d (%+d)\n\n", p_money, p_diff, h_money, h_diff);

            round++;
            total_round++;

            if (p_money >= target_total_money) {
                break;
            }
        }

        double p_win_rate_day = (double)p_win_count * 100 / (p_win_count + h_win_count);
        double h_win_rate_day = (double)h_win_count * 100 / (p_win_count + h_win_count);

        total_p_win_count += p_win_count;
        total_h_win_count += h_win_count;

        if (p_streak > total_p_streak) total_p_streak = p_streak;
        if (h_streak > total_h_streak) total_h_streak = h_streak;

        fprintf(fp, "--- จบวันที่ %d ---\n", day);
        fprintf(fp, "P ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", p_win_count, p_max_streak);
        fprintf(fp, "H ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", h_win_count, h_max_streak);
        fprintf(fp, "เปอร์เซ็นต์ชนะประจำวันของ P: %.2f%%\n", p_win_rate_day);
        fprintf(fp, "เปอร์เซ็นต์ชนะประจำวันของ H: %.2f%%\n\n", h_win_rate_day);

        printf("--- จบวันที่ %d ---\n", day);
        printf("P ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", p_win_count, p_max_streak);
        printf("H ชนะวันนี้ %d ครั้ง | ชนะติดกันสูงสุด = %d\n", h_win_count, h_max_streak);
        printf("เปอร์เซ็นต์ชนะประจำวันของ P: %.2f%%\n", p_win_rate_day);
        printf("เปอร์เซ็นต์ชนะประจำวันของ H: %.2f%%\n\n", h_win_rate_day);

        if (p_money >= target_total_money || (h_money <= 0 && p_money > 0)) {
            if (p_money >= target_total_money) {
                fprintf(fp, "### ผู้เล่นชนะ! เพราะทำกำไรถึงเป้าหมาย %d บาท ในวันที่ %d ###\n\n", target_total_money, day);
                printf("### ผู้เล่นชนะ! เพราะทำกำไรถึงเป้าหมาย %d บาท ในวันที่ %d ###\n\n", target_total_money, day);
            } else {
                fprintf(fp, "### ผู้เล่นชนะ! เจ้ามือหมดตัวในวันที่ %d ###\n\n", day);
                printf("### ผู้เล่นชนะ! เจ้ามือหมดตัวในวันที่ %d ###\n\n", day);
            }

            double p_win_rate_total = (double)total_p_win_count * 100 / total_round;
            double h_win_rate_total = (double)total_h_win_count * 100 / total_round;

            fprintf(fp, "=== สรุปรวมตั้งแต่วันแรกจนวันที่ %d ===\n", day);
            fprintf(fp, "รอบที่เล่นทั้งหมด: %d\n", total_round);
            fprintf(fp, "P ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_p_win_count, total_p_streak);
            fprintf(fp, "H ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_h_win_count, total_h_streak);
            fprintf(fp, "เปอร์เซ็นต์ชนะรวมของ P: %.2f%%\n", p_win_rate_total);
            fprintf(fp, "เปอร์เซ็นต์ชนะรวมของ H: %.2f%%\n\n", h_win_rate_total);

            printf("=== สรุปรวมตั้งแต่วันแรกจนวันที่ %d ===\n", day);
            printf("รอบที่เล่นทั้งหมด: %d\n", total_round);
            printf("P ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_p_win_count, total_p_streak);
            printf("H ชนะทั้งหมด: %d ครั้ง | ชนะติดกันสูงสุดรวม: %d\n", total_h_win_count, total_h_streak);
            printf("เปอร์เซ็นต์ชนะรวมของ P: %.2f%%\n", p_win_rate_total);
            printf("เปอร์เซ็นต์ชนะรวมของ H: %.2f%%\n\n", h_win_rate_total);

            fclose(fp);

            char final_filename[150];
            snprintf(final_filename, sizeof(final_filename),
                     "/storage/emulated/0/Download/CodingC/%d_ชนะวันที่ %d.txt",
                     initial_p_money + initial_h_money, day);
            rename(temp_filename, final_filename);

            break;
        }

        day++;
    }

    return 0;
}