小诺诺喜欢玩纸牌比大小的游戏。现在有一副牌中的若干张纸牌,需要按牌面的数字从大到小的顺序排列,若数字大小相同则按花色从大到小(黑桃>红桃>梅花>方块)排列。牌面为A、J、Q、K分别用1、11、12、13表示;花色中的黑桃、红桃、梅花、方块分别用英文单词"spade"、"heart"、"club"、"diamond"表示。
输入格式:测试数据有多组,首先输入测试的组数T (0<T<10),然后是T组测试数据;每组测试输入一行,按“花色 数字”的格式输入若干张牌,花色可能为"spade"、"heart"、"club"、"diamond"之一,数字为1~13。输入的数据之间可能有若干(至少1个)空格,在行的首尾也可能有若干空格,但每组输入数据的总长度不会超过1000个字符。
输出格式:
每组测试输出一行,按描述中的排序规则从大到小输出牌的信息,数据之间都以一个空格分隔。
输入样例:
2
diamond 1 club 1 heart 1 spade 3 diamond 2 club 3
diamond 13 club 13 heart 13 spade 12
思路:
1.定义二维数组,保存所有输入的值;
2.使用scanf方法输入,用空格分隔;
3.循环读取数据到数组。
如下:
#include <stdio.h>
#include <stdlib.h>
//定义每一行中最多输入的牌数
#define MAXNMB 20
struct Pai
{
char huase[10];
int nmb;
struct Pai* next;
};
int main()
{
int T;
char ch,tmp[4]={0};
int i,j,k;
struct Pai* st[10]; //T小于10,所以这里定义10个结构体
struct Pai *p1,*p2;
scanf("%d",&T);
for(i = 0;i< T;i++)
{
struct Pai *head = (struct Pai *)malloc(sizeof(struct Pai));
head->next = 0;
p1 = head;
while(true)
{
j = 0;
k = 0;
ch = getchar();
if(ch == ' ')
{
if(k != 0)
{
p1->nmb = atoi(tmp);
k = 0;
j = 0;
p2 = (struct Pai *)malloc(sizeof(struct Pai));
p2->next = 0;
p1->next = p2;
p1 = p2;
}
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
p1->huase[j++] = ch;
else if(ch >= '0' && ch <= '9')
tmp[k++] = ch;
if(ch == '\n')
{
if(k != 0)
p1->nmb = atoi(tmp);
break;
}
}
st[i] = head;
}
//完成工作以后要释放空间,这里就不写了
return 0;
}