Chinaunix首页 | 论坛 | 博客
  • 博客访问: 687479
  • 博文数量: 156
  • 博客积分: 3402
  • 博客等级: 中校
  • 技术积分: 1639
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-13 14:06
个人简介

业余编程爱好者

文章分类

全部博文(156)

文章存档

2014年(1)

2013年(13)

2012年(46)

2011年(38)

2010年(58)

分类: LINUX

2012-05-29 14:50:45

    发布一个刮刮乐游戏,模拟真实情况下个刮刮乐彩票。先输入总资金,再输入每次使用的资金,在win平台和linux平台上的GCC编译通过。

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. // #include<windows.h>
  5. const int award[10] = {5,10,15,20,30,50,100,500,1000,10000};
  6. static int prize_pool[10000];

  7. int main()
  8. {    
  9.     int init_sum;
  10.     int avail_sum;
  11.     int coin_money;
  12.     int sum_award_money;
  13.     
  14.     generate_prize_pool();
  15. //    look_prize_pool();

  16.     printf("Your initial capital : ");
  17.     scanf("%d",&init_sum);
  18.     printf("\n");
  19.     avail_sum = init_sum;
  20.     
  21.     while(avail_sum > 0) {
  22.         printf("Start!Please coin: ");        
  23.         scanf("%d",&coin_money);
  24.         printf("\n");
  25.         avail_sum -= coin_money;
  26.         
  27.         if(avail_sum < 0) {
  28.             avail_sum += coin_money;
  29.             printf("Your available funds too less! Try again!\n");
  30.             continue;
  31.         }
  32.         
  33.         srand((unsigned)time(0));
  34.         sum_award_money = make_lottery(coin_money);         
  35.         avail_sum += sum_award_money;    
  36.         
  37.         printf("You win %d yuan!!!\n",sum_award_money);
  38.         printf("Your available funds : %d yuan\n",avail_sum);
  39.     }
  40.     
  41.     printf("Sorry! You have no money!!\n");
  42.     system("pause");
  43.     return 0;
  44. }

  45. int make_lottery(int money)
  46. {
  47.     int i;
  48.     int win_num;
  49.     int you_num;
  50.     int award_money;
  51.     int sum_award_money = 0;
  52.     int max_num = 15;        /*6.6% Probability of winning */
  53.         
  54.     printf("WinNumber\tYouNumber\tAwardMoney\n");
  55.     for(i = 0; i < money; i++) {
  56.         you_num = roll(max_num);
  57.         win_num = roll(max_num);
  58.         
  59.         printf("%d\t\t%d\t\t", win_num, you_num);
  60.         if(you_num == win_num) {
  61.             award_money = prize_pool[roll(10000)];
  62.             sum_award_money += award_money;
  63.             printf("%d\tWin!",award_money);
  64.         }
  65.         else {
  66.             award_money = award[roll(10) - 1];
  67.             printf("%d\tLose!",award_money);
  68.         }
  69.         printf("\n");
  70. //        Sleep(1000);        
  71.     }    
  72.     return sum_award_money;
  73. }

  74. int roll(int max_num)
  75. {
  76.     return 1+rand()%max_num;
  77. }

  78. int generate_prize_pool()
  79. {
  80.     int i,j,k,temp;
  81.     
  82.     int award_num[10] = {7370,1000,666,500,333,200,100,20,10,1};
  83.     
  84.     for(i=0; i<10000; i++)
  85.         prize_pool[i] = 5;
  86.     for(j=0; j<10; j++)
  87.         for(k=0; k<award_num[j]; k++){
  88.             temp = roll(10000);
  89.             prize_pool[temp] = award[j];
  90.         }
  91.     return 0;    
  92. }

  93. int look_prize_pool()
  94. {
  95.     int i;
  96.     for(i=0; i<10000; i++)
  97.         printf("%d ",prize_pool[i]);
  98.     printf("\n");
  99.     return 0;
  100. }

阅读(970) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~