扑克牌比大小
扑克牌式数字排序扑克牌中有以下几个牌面:3 4 5 6 7 8 9 0(0代表10) J Q K A 2 S(代表司令),假定有一些数字是由如上的扑克牌牌面组成,则假设3<4<5<6<7<8<9<0<J<Q<K<A<2<S,给定一组扑克牌数,请按照从小到大的顺序进行排序!
例如:给定数字序列234 589 523 KS2 K2Q 2KS J98 0KA,则排序后的结果为:589<523<0KA<J98<K2Q<KS2<234<2KS,演示代码稍后会发放!
#include <stdio.h> #include <stdlib.h> int getint(char c) { if (c >= '3' && c <= '9') { return c - '0'; } switch (c) { case '0': return 10; case 'J': return 11; case 'Q': return 12; case 'K': return 13; case 'A': return 14; case '2': return 15; case 'S': return 16; } return 0; } int mystrcmp(const char *s1, const char *s2) { int cmp = 0; while (*s1) { cmp = getint(*s1) - getint(*s2); if (cmp != 0) return cmp; s1++; s2++; } return cmp; } int main() { char pk[8][4] = {"234", "589", "523", "KS2", "K2Q", "2KS", "J98", "0KA"}; int pkIndex[8] = {0, 1, 2, 3, 4, 5, 6, 7}; for (int i = 0; i < 7; i++) { for (int j = i; j < 8; j++) { if (mystrcmp(pk[pkIndex[i]], pk[pkIndex[j]]) > 0) //对调字符串,拿索引打捷路。 { int t = pkIndex[i]; pkIndex[i] = pkIndex[j]; pkIndex[j] = t; } } } for (int i = 0; i < 8; i++) { printf("%s ", pk[pkIndex[i]]); } printf("\n"); return 0; }
#include <stdio.h> #include <string.h> void main() { int i, j, n; int map[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,13,1,2,3,4,5,6,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,11,0,0,0,0,0,10,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; char result[4096][4] = {0}; int count[4096] = {0}; char pk[1024][4]; printf("请输入序列个数:"); scanf("%d", &n); printf("请输入所有序列:\n"); for (i=0; i<n; i++) { scanf("%s", pk[i]); } for (i=0; i<n; i++) { j = (map[pk[i][0]]<<8) | (map[pk[i][1]]<<4) | (map[pk[i][2]]); memcpy(result[j], pk[i], 4); count[j]++; } for (i=0; i<4096; i++) { if (result[i][0]) { for (j=0; j<count[i]; j++) { printf("%s ", result[i]); } } } printf("\n"); }