C++ 5张不同卡牌组合对应返回
2014-09-20 15:02
127 查看
#include <iostream>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <ctime>
using namespace std;
static const int CARD_NUM = 5;
static const int CARD_CHOICE = 6;
void output(int *cont, int len, int start = 0);
int spades_ret_val(int *card, int num);
void test_spades();
void spades();
int main()
{
test_spades();
spades();
system("pause");
return 0;
}
void test_spades()
{
//123456
//等级 组合
描述
// 1 5张黑桃A
5张牌都是黑桃A
// 2 5张相同(但不是A)
5张卡片完全相同,但不是黑桃A
// 3 4+1
卡牌有四张相同,加上额外的一张
// 4 5连顺
卡牌5张连续
// 5 3+2
卡牌有三张相同,加上额外的一对
// 6 4连顺
卡牌有4张连续的
// 7 3+1+1
卡牌有三张相同的,再加上额外的两张不同的
// 8 2+2+1
卡牌有两对,加上一张不同卡牌
// 9 2+1+1+1
卡牌有一对,加上三张不同卡牌
// 10 散牌
卡牌既不连续,也无相同
int card_val[10][CARD_NUM] =
{
{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{2, 2, 2, 2, 3},
{2, 5, 4, 3, 6},
{1, 5, 1, 5, 5},
{4, 1, 6, 2, 3},
{1, 4, 1, 5, 1},
{1, 3, 1, 5, 3},
{1, 4, 1, 5, 6},
{1, 3, 2, 5, 6},
};
for (int i = 0; i < 10; i++)
{
cout << spades_ret_val(card_val[i], CARD_NUM) << endl;
}
}
int spades_ret_val(int *card, int num)
{
if (num != CARD_NUM)
{
return -1;
}
output(card, CARD_NUM, 0 );
int card_aux[CARD_NUM + 1] = {-1};
int card_adj[CARD_NUM + 1] = {-1};
map<int, int> card_map;
for (int i = 0; i < CARD_NUM; i++)
{
card_aux[i + 1] = card[i];
card_map[ card[i] ]++;
}
sort(card_aux, card_aux + CARD_NUM + 1);
//output(card_aux, CARD_NUM + 1, 1 );
map<int, int>::iterator it = card_map.begin();
adjacent_difference(card_aux, card_aux + CARD_NUM + 1, card_adj);
//output(card_adj, CARD_NUM + 1);
int return_val = -1;
int count_adj_one = (int)count(card_adj, card_adj + CARD_NUM + 1, 1);
//cout << count_adj_one << endl;
switch((int)card_map.size())
{
case 1:
return_val = card[0] == 1 ? 1 : 2;
break;
case 2:
return_val = (it->second == 1 || it->second == 4) ? 3 : 5;
break;
case 3:
return_val = (it->second * (++it)->second) % 2 ? 7 : 8;
break;
case 4:
return_val = count_adj_one == 3 ? 6 : 9;
break;
case 5:
return_val = count_adj_one == 4 ? 4 : 10;
break;
default:
//error
break;
}
return return_val;
}
void output(int *cont, int len, int start)
{
if (start < 0 || start >= len) return;
for (int i = start; i < len; i++)
{
cout << (*(cont + i)) << "\t";
}
cout << ":\t";
}
void spades()
{
int card[CARD_NUM + 1] = {-1};
int card_adj[CARD_NUM + 1] = {-1};
while (1)
{
cin.get();
srand( (unsigned)time( NULL ) );
for (int i = 0; i < CARD_NUM; i++)
{
card[i + 1] = rand() % CARD_CHOICE + 1;
}
cout << spades_ret_val(card + 1, CARD_NUM) << endl;
}
}
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <ctime>
using namespace std;
static const int CARD_NUM = 5;
static const int CARD_CHOICE = 6;
void output(int *cont, int len, int start = 0);
int spades_ret_val(int *card, int num);
void test_spades();
void spades();
int main()
{
test_spades();
spades();
system("pause");
return 0;
}
void test_spades()
{
//123456
//等级 组合
描述
// 1 5张黑桃A
5张牌都是黑桃A
// 2 5张相同(但不是A)
5张卡片完全相同,但不是黑桃A
// 3 4+1
卡牌有四张相同,加上额外的一张
// 4 5连顺
卡牌5张连续
// 5 3+2
卡牌有三张相同,加上额外的一对
// 6 4连顺
卡牌有4张连续的
// 7 3+1+1
卡牌有三张相同的,再加上额外的两张不同的
// 8 2+2+1
卡牌有两对,加上一张不同卡牌
// 9 2+1+1+1
卡牌有一对,加上三张不同卡牌
// 10 散牌
卡牌既不连续,也无相同
int card_val[10][CARD_NUM] =
{
{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{2, 2, 2, 2, 3},
{2, 5, 4, 3, 6},
{1, 5, 1, 5, 5},
{4, 1, 6, 2, 3},
{1, 4, 1, 5, 1},
{1, 3, 1, 5, 3},
{1, 4, 1, 5, 6},
{1, 3, 2, 5, 6},
};
for (int i = 0; i < 10; i++)
{
cout << spades_ret_val(card_val[i], CARD_NUM) << endl;
}
}
int spades_ret_val(int *card, int num)
{
if (num != CARD_NUM)
{
return -1;
}
output(card, CARD_NUM, 0 );
int card_aux[CARD_NUM + 1] = {-1};
int card_adj[CARD_NUM + 1] = {-1};
map<int, int> card_map;
for (int i = 0; i < CARD_NUM; i++)
{
card_aux[i + 1] = card[i];
card_map[ card[i] ]++;
}
sort(card_aux, card_aux + CARD_NUM + 1);
//output(card_aux, CARD_NUM + 1, 1 );
map<int, int>::iterator it = card_map.begin();
adjacent_difference(card_aux, card_aux + CARD_NUM + 1, card_adj);
//output(card_adj, CARD_NUM + 1);
int return_val = -1;
int count_adj_one = (int)count(card_adj, card_adj + CARD_NUM + 1, 1);
//cout << count_adj_one << endl;
switch((int)card_map.size())
{
case 1:
return_val = card[0] == 1 ? 1 : 2;
break;
case 2:
return_val = (it->second == 1 || it->second == 4) ? 3 : 5;
break;
case 3:
return_val = (it->second * (++it)->second) % 2 ? 7 : 8;
break;
case 4:
return_val = count_adj_one == 3 ? 6 : 9;
break;
case 5:
return_val = count_adj_one == 4 ? 4 : 10;
break;
default:
//error
break;
}
return return_val;
}
void output(int *cont, int len, int start)
{
if (start < 0 || start >= len) return;
for (int i = start; i < len; i++)
{
cout << (*(cont + i)) << "\t";
}
cout << ":\t";
}
void spades()
{
int card[CARD_NUM + 1] = {-1};
int card_adj[CARD_NUM + 1] = {-1};
while (1)
{
cin.get();
srand( (unsigned)time( NULL ) );
for (int i = 0; i < CARD_NUM; i++)
{
card[i + 1] = rand() % CARD_CHOICE + 1;
}
cout << spades_ret_val(card + 1, CARD_NUM) << endl;
}
}
相关文章推荐
- UML中定义的关系主要有6种: 依赖、关联、聚合、组合/合成、泛化、实现; 以及对应的C++体现
- c++ 递归实现不同字母的全排列组合
- [2011-11-23] 教训:C++的DLL和C#采用不同的内存管理,不要返回指向堆的指针,而改成读写文件
- C++函数返回值不同写法的效率探讨
- C++中不同类型变量对应的字节数
- C++DoModal怎样根据不同的按钮返回不同的值?
- 仅返回类型不同的函数,在C++中如何实现重载?
- Gson解析时,在返回相同的实体,但对应的字段不同(可能是不同部门的api)
- 给定一个数字字符串,返回数字可能表示的所有可能的字母组合。数字与字母的对应关系和手机按键对应
- 给定一个数字字符串,返回数字可能表示的所有可能的字母组合。数字与字母的对应关系和手机按键对应。
- 仅返回类型不同的函数,在C++中如何实现重载?
- C++中函数定义返回类型和内部实现返回类型不同的原因
- web服务器 http请求返回 不同返回码的 对应 语意
- [C++学习笔记14]动态创建对象(定义静态方法实现在map查找具体类名对应的创建函数,并返回函数指针,map真是一个万能类)good
- C++12个高矮不同的人,排成两排,每排必须是从矮到高排列,而且第二排比对应的第一排的人高,问排列方式有多少种?
- c++ 只有返回值不同类成员函数重载
- 《读java编程思想》 c++与java的不同(C++进阶 继承 组合 多态)
- 函数的返回类型可以为值、指针、引用,这三种返回类型有什么不同,各有什么好处?(c++)
- c++和python string 的find()方法的返回值不同
- C++中函数定义返回类型和内部实现返回类型不同的原因