Chinaunix首页 | 论坛 | 博客
  • 博客访问: 444504
  • 博文数量: 67
  • 博客积分: 2468
  • 博客等级: 上尉
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-05 01:21
文章分类

全部博文(67)

文章存档

2013年(1)

2012年(65)

2011年(1)

分类: C/C++

2012-07-26 09:26:45

点击(此处)折叠或打开

  1. /*判定扑克的牌型。给5张牌,返回它的牌型,
  2.  *顺子:case 55
  3.  *4个的+1个单张:case 41
  4.  *3个+对子:case 32
  5.  *3个+2个单张:case 311
  6.  *2对+1个单张:case 221
  7.  *1对+3个单张:case 2111
  8.  *5个单张:case 51
  9.  */
  10. #include <iostream>
  11. #include <cstdlib>

  12. using namespace std;

  13. void sort_cards(const int *cards, int *cards_sorted, int num);

  14. int is_straight(int *my_cards, int start, int end);//case 55
  15. int is_4same1(int *my_cards, int start, int end);//case 41
  16. int is_3same2same(int *my_cards, int start, int end);//case 32

  17. int
  18. main(int argc, char *argv[])
  19. {
  20.     int cards[] = {5, 4, 1, 3, 2};
  21.     int cards_sorted[5];
  22.     int num = 5;

  23.     sort_cards(cards, cards_sorted, num);
  24.     if(is_straight(cards_sorted, 0 , 4))
  25.         cout << "\n cards are straight.55" << endl;
  26.     if(is_4same1(cards_sorted, 0, 4))
  27.         cout << "\n cards are 4same1.41" << endl;
  28.     int result32 = is_3same2same(cards_sorted, 0 ,4);
  29.     switch (result32){
  30.         case 5:
  31.             cout << "\n cards are 3same2same.32" << endl;
  32.             break;
  33.         case 4:
  34.             cout << "\n cards are 2match1different.221" << endl;
  35.             break;
  36.         case 3:
  37.             cout << "\n cards are 3same2different.311" << endl;
  38.             break;
  39.         case 2:
  40.             cout << "\n cards are 1match3different.2111" << endl;
  41.             break;
  42.         case 0:
  43.             cout << "\n cards are 5different.51" << endl;
  44.             break;
  45.         default:
  46.             break;
  47.     }

  48.     return 0;
  49. }

  50. /*C++功底不够,还是有很多C的影子*/
  51. void
  52. sort_cards(const int *cards, int *cards_sorted, int num){
  53.     int i, j, min_value, min_position, tmp_value;
  54.     for(i = 0; i < num; i++){
  55.         *(cards_sorted + i) = *(cards + i);
  56.     }

  57.     for(i = 0; i < num; i++){
  58.         min_value = *(cards_sorted + i);
  59.         min_position = i;
  60.         for(j = i+1; j < num; j++){
  61.             if(*(cards_sorted + j) < min_value){
  62.                 min_value = *(cards_sorted + j);
  63.                 min_position = j;
  64.             }
  65.         }
  66.         tmp_value = *(cards_sorted + i);
  67.         *(cards_sorted + i) = min_value;
  68.         *(cards_sorted + min_position) = tmp_value;
  69.     }

  70.     cout << "cards_sorted are:" << endl;
  71.     for(i = 0; i < num; i++){
  72.         cout << *(cards_sorted + i) << " ";
  73.     }
  74. }

  75. int
  76. is_straight(int *my_cards, int start, int end)//case 55
  77. {
  78.     int i;
  79.     for(i = 0; i < 4; i++){
  80.         if(*(my_cards + start) < 3)
  81.             return 0;
  82.         if(*(my_cards + start + i + 1) - *(my_cards + start + i) != 1)
  83.             return 0;
  84.     }
  85.     return 1;
  86. }

  87. int
  88. is_4same1(int *my_cards, int start, int end)//case 41
  89. {
  90.     int i, j;
  91.     for(i = start; i < (end -3); i++){
  92.         for(j = 0; j < 3; j++){
  93.             if(*(my_cards + i + j + 1) != *(my_cards + i + j))
  94.                 break;
  95.             if(j == 2)
  96.                 return 1;
  97.         }
  98.     }
  99.     return 0;
  100. }

  101. /*这个函数的设计还是比较巧妙,根据返回值,得到不同的类型。
  102.  */
  103. int
  104. is_3same2same(int *my_cards, int start, int end)//case 32
  105. {
  106.     int i, j;
  107.     int hit3 = 0;
  108.     int hit2 = 0;
  109.     for(i = start; i < end;){
  110.         for(j = 0; j < 2; j++){
  111.             if(*(my_cards + i + j + 1) != *(my_cards + i + j))
  112.                 break;
  113.         }
  114.         if(j == 2){
  115.             hit3 += 3;
  116.             i += 3;
  117.             continue;
  118.         }
  119.         if(j == 1){
  120.             hit2 += 2;
  121.             i += 2;
  122.             continue;
  123.         }
  124.         i++;
  125.     }

  126.     return (hit2 + hit3);
  127. }

  128. /*还有很多需要改进的地方。真想做个斗地主。或许应该找个斗地主的源码学习下。
  129.  *Look, before I If I don't know the direction, I should do some small things.
  130.  */

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